javaandroidxmlviewandroid-inflate

Can someone explain inflate method? Deeper understanding of Android views


I want deeper understanding of how Android works, and I need someone to explain how Views are actually working "under the hood".

In normal procedure we would inflate (is this the correct word?) views from XML in onCreate method of our extended Activity with method "setContentView(R.layout.ourlayoutfile)". Then we would find Views from that XML.

Quick example: If we need to find a button we need first call "setContentVIew()" and then, "findViewById" on our button. Then we can work with this button / view respectively.

I've started to play with LayoutInflater recently, because I came to the point which I couldn't help myself with "setContentView" method, and on my surprise I found out that my Android knowledge sucks very good. I couldn't even manage LayoutInflater to work. I was embarrassed.

After a day I manage to inflate views with LayoutInflater. Actually it's not very hard, I was very close already from the start BUT there was one parameter which I didn't know what to pass in. Please look at this code: ( This is all happening in onCreate method of Activity )

View v = getLayoutInflater().inflate(R.layout.activity_main, (ViewGroup) getWindow().getDecorView());
    
    
    
    final Button b = (Button) v.findViewById(R.id.button1);
    final TextView tv = (TextView) v.findViewById(R.id.textView1);
    
    b.setOnClickListener(new View.OnClickListener() {
        
        @Override
        public void onClick(View v) {

            tv.setText("Just random text");
        }
    });
    
}

This code works fine, but the problem I was facing was actually this line of code:

getLayoutInflater().inflate(R.layout.activity_main, null);

I was always passing "null" parameter, and of course it didn't worked. Even tho in documentation it says that this parameter is OPTIONAL! ( Why?, if it's needed)?

I've made simple layout. Please look at it and how it looks with HiearchyViewer:

What is the second parameter in picture above, and why do we need it in there? Is maybe connecting my layout ( R.layout.activity_main ), with View provided by Android ( First view from left to right - parent view ). If that's the case, WHY doesn't android connect these two automatically?!

If there is also something useful I need to know regarding Views I will be very glad if someone could tell me ( or post a link ). Additionally would be nice if I could get some links to some websites of How Views work.. etc. Useful stuff.


Solution

  • As you might already know by now that every visible component in Android is a View. That includes Button, Spinner, TextView, EditText and the likes. You are also right about the way we access the Views that are defined in a xml file in our program, by inflating it and then finding the view by using it's id. The usual way to do this is by using setContentView() method.

    But there is this pretty useful class called LayoutInflater that can be used to "inflate" a layout. Now consider the scenario where you have a ListView in your xml file that you inflated using setContentView() method. In this ListView you want the items to contain an ImageView and a TextView. The default list-item just have a TextView. So you decided to write up a custom adapter in which you'll be using a new listitems.xml in which you have a TextView and ImageView defined. Now you can't use setContentView() in this case as it will inflate this layout on the whole activity which obviously you don't want. So you use LayoutInflater in this case to help you out. You temporarily inflate a layout using inflate() method. The first argument takes the layout file which is needed to be inflated. The second argument is the root of this newly inflated layout. In our case it can be set to the ListView in which the layout is going to be actually inflated.

    So when you use this inflate() method, a View is returned containing the views that are inside the inflated xml. Now with this returned view instance you can call findViewById() to get the contained views to set text to the TextView and image source to the ImageView.

    More often than not, you'll end up using LayoutInflater as it's use cases are wider than the scope of this discussion.