1

For example When we write the code

View view = inflater.inflate(R.layout.main_activity, null);

What does the Android system do?

Reno
  • 33,594
  • 11
  • 89
  • 102
Gaurav Agarwal
  • 18,754
  • 29
  • 105
  • 166
  • A little comment line will help me understand the downvotes. – Gaurav Agarwal Dec 24 '12 at 16:39
  • Are you asking for where in the AOSP this happens or, how does the process work? If how the process works, what specific pieces? How the layout is measured? How the view objects are instantiated? Something else? – Simon Dec 24 '12 at 16:44
  • The primary question is how it is done and secondary question is any code references or links will be useful in understanding the process. – Gaurav Agarwal Dec 24 '12 at 16:45
  • It's hard to tell what you're asking. In the question, you ask the meaning of the term. In the comments on an answer, you say you know what it means and wish to know how it's done. Also, basically every Android tutorial(both official and unofficial) shows how to inflate an xml file in the most basic sense. – Geobits Dec 24 '12 at 16:46
  • @Geobits Tutorials tell define your layout in XML and do `findViewById` in the code. But a lot of things must going on behind the scene. That is what the question is about? – Gaurav Agarwal Dec 24 '12 at 16:48
  • Then you should probably rephrase the question to ask what happens behind the scenes, instead of opening with "What is the meaning of the term..." – Geobits Dec 24 '12 at 16:49

2 Answers2

4

Check out the source for the LayoutInflater. It's an abstract class, a concrete instance of which is obtained through getLayoutInflater().

In essence, the inflater creates a root view object (the root view group of the inflated XML), then does two passes through the XML tree to attach each child view. This is done recursively to handle 'include' and to fix up references between child views, for example in RelativeLayout, and is done top to bottom.

The first pass constructs the tree by instantiating each of the child views, top down recursively, and passes the XML attributes to the view constructor telling the view how big it should be. It then calls measure() for each child passing in restrictions determined by the parent (e.g. RelativeLayout with 2 child views each requesting match_parent) using a measure specifications object and asks the view how big it wants to be. If the view is itself a view group, it will use the same algorithm to measure it's children.

The second pass is the layout pass when layout() is called on each child to position itself within the view. The parent positions the view using the measurements calculated in the measure pass. onDraw() is called and is passed a Canvas created from the DecorView backing bitmap.

The finalised tree is then ready to pass to the window manager which is done by setContentView() or addContentView().

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.1_r1/android/view/LayoutInflater.java#LayoutInflater

Simon
  • 14,407
  • 8
  • 46
  • 61
  • Simon, my understanding is that `LayoutInflater` only creates `View` objects from `xml` file and adds child views to the parent. Measure, layout, and draw passes are not responsibility of `LayoutInflater`. Can you please correct me if I'm wrong? – Sabeeh May 14 '17 at 23:04
0

Inflating an XML layout in simple language means you are converting the XML in View. Then you can fetch each view declared in the XML using the parent/inflated View.

For eg -

View view = inflater.inflate(R.layout.main_activity, null);

Now, here view is the reference of the XML from which you can fetch all the views as,

TextView tv = (TextView)view.findViewById(R.id.tv);
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242