Home » Android » android – what is the use of the layoutinflator?

android – what is the use of the layoutinflator?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m a new Android developer. I have tried to understand the use of Layout inflator from the documentation but have not been able to understand. what is the use of layout inflator in android?

What are the situations when one would have to use it?

How to&Answers:

More Technical Explanation

See this

Reworded for Simplicity


Basically think of your xml flat files as a balloon without air and, what the LayoutInflator does is blows it up (or inflates) into a full sized balloon a.k.a. your Views and, or View groups. So flat file gets inflated into view. (feel free to critique the metaphor but, that is how I think of it anyway) and as far as usage goes check this article out. The author talks about manually inflation of objects.

Somewhat oversimplified explanation of how this is implemented.

Internally an xml parser, that is mapped to a cached subset of the Android.Resource, looks for applicable xml that is recognized as a view instantiates them and adds them to the view group.

Where to look

From that internally rInflate in LayoutInflator parses the xml file and adds the result to the parent view. Look for the overload with the specific signature

void rInflate(XmlPullParser parser, View parent, final AttributeSet attrs, boolean finishInflate)

Additional things of note

In the comments ofLayoutInflater

For performance
* reasons, view inflation relies heavily on pre-processing of XML files
* that is done at build time. Therefore, it is not currently possible to
* use LayoutInflater with an XmlPullParser over a plain XML file at runtime.

So with that we can infer that the xml from the file is cached at compile time to cut the amount of processing required to handle parsing the file.

I hope this clears things up a bit more.

Answer:


Layout Inflator in android is a very important part of Android


                           _ _ _ _ _ _ _ _ _ _ _ _
                          |                       |
                          |       _____________   |            
                          | Name |_____________|--|-----<2 objects(TextView,EditText)> 
                          |       _____________   |            
                          | Age  |_____________|--|-----<2 objects(TextView,EditText)> 
                          |       _____________   |             
                          | Phone|_____________|--|-----<2 objects(TextView,EditText)> 
                          |                       |
                          |                       |
                          |                       |
                          |                       |
                          |        ------         |         
                          |       |BUTTON| -------|-----<1 objects(ButtonView)>      
                          |        ------         |
                          |                       |
                           -----------------------                           

We can clearly see from the above figure,

  • The UI elements on the activity are displayed & behind the activity which is not visible for the user complex actions are taking place by
    android system

What are these complex actions ?

  • Simple way we can describe is that, Objects are created for each views that are existing on the android XML file and the instantiation
    is made here.
  • With the help of a Inflator service thse object creation is done & this process is called layout Inflation