what is the use of the layoutinflator?

user590849 picture user590849 · Feb 22, 2011 · Viewed 7.8k times · Source

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?

Answer

Terrance picture Terrance · Feb 22, 2011

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.