what is the use of the layoutinflator?
Asked Answered
L

2

8

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?

Lucifer answered 22/2, 2011 at 9:50 Comment(1)
Just like you develop xml parsers that convert certain xml into meaningful objects, html renderers that render html text pages into complete rendered web pages, or even translators that translate incomprehensible words into words you understand, a LayoutInflator simply converts the layout resource file (xml) into View objects. While doing this task, it also hooks these view with your activity's context and of course validates your xml layout.Cienfuegos
S
17

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.

  • For a detailed view on how the cached data is pulled see XmlResourceParser loadXmlResourceParser(String file, int id, int assetCookie, String type) in the file Resources.java.
  • For the Layout Inflator source see LayoutInflater.java
  • For the ways you can browse the android os source code see this question

I hope this clears things up a bit more.

Scheers answered 22/2, 2011 at 18:9 Comment(1)
that's a nice explanation.. i felt like answering but then felt that you already did a lovely job. its an up-vote from me.. :)Reprimand
B
2

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
Bouffant answered 11/9, 2013 at 3:13 Comment(4)
Mind if I ask your reason(s) for starting a bounty on this question, and then deciding to answer it yourself?Hazelhazelnut
I was researching about this Question, I could not find good answers for this in stackoverflow. Studying this topic got me some idea about the question, so i answered it. But i have kept the bounty open to see more quality answer than the one i posted here.Bouffant
So I take it to mean you didn't like my answer or, find it particularly helpful then?Scheers
@ Terrance .... You simplified your answer to be better than before ..... Thanks, 100 Bounty points from my pocket to you :)Bouffant

© 2022 - 2024 — McMap. All rights reserved.