I am new to android development and keep coming across references to Inflating views from a layout xml file. I googled and searched the development guide but still wasn't able to pick up a sense for what it means. If someone could provide a very simple example, it'd be much appreciated.
7 Answers
When you write an XML layout, it will be inflated by the Android OS which basically means that it will be rendered by creating view object in memory. Let's call that implicit inflation (the OS will inflate the view for you). For instance:
class Name extends Activity{
public void onCreate(){
// the OS will inflate the your_layout.xml
// file and use it for this activity
setContentView(R.layout.your_layout);
}
}
You can also inflate views explicitly by using the LayoutInflater
. In that case you have to:
- Get an instance of the
LayoutInflater
- Specify the XML to inflate
- Use the returned
View
- Set the content view with returned view (above)
For instance:
LayoutInflater inflater = LayoutInflater.from(YourActivity.this); // 1
View theInflatedView = inflater.inflate(R.layout.your_layout, null); // 2 and 3
setContentView(theInflatedView) // 4
-
30if inflate = rendering, then what is the use of onDraw(), precisely ? – PypeBros Jul 19 '12 at 09:44
-
@sylvainulg, The R.id.view is used to change the attributes of the xml element and inflate can do the same. inflating is particularly useful in custom views. To inflate the entire xml file, the familiar setContentView of Activity class is used. A View must inflate each View manually using LayoutInflater object.inflate(). An Activity has a Life Cycle. A View has a draw cycle instead. inflater is particulary useful with custom view instead of using predifiend layout in XML file. – Sree Rama Apr 27 '13 at 06:06
-
@Cristian Please, do you think you could give me some advice/ideas over here http://goo.gl/E37NSu – eddy Mar 01 '15 at 15:26
-
after inflating in the onclick of particular item how to get those values. – Harish Aug 17 '15 at 12:13
-
30I don't think inflating is rendering rather inflating is actual creation of View object in memory based on the xml file contents. After the inflate the View will be in memory not necessarily visible. – Ahmed Dec 11 '15 at 06:40
-
1Maybe render has two meanings? Like render from a layout file to an object, and render on the screen. Similar to web development, you render a template to an html page, then the browser renders that page on the screen. Maybe the answer is about the first meaning and comments about the second? Or maybe it's wrong, I don't know. – Mark Apr 04 '17 at 20:20
-
@cristian I see the answer has received praise and upvotes, but there is still a key issue unaddressed: if it really is true that "when you write an XML layout, it will be inflated by the Android OS which basically means that it will be rendered by creating view object in memory.", then why do we need to call findViewById()? Are the "view objects in memory" created without making them accessible to the Java namespace until findViewById is called?? – Matt J. Apr 30 '17 at 01:21
-
5@MattJ. Well, my answer might be poorly phrased. Android will _inflate_ the view not just by having the XML somewhere, but when you call setContentView or manually inflate the layout. By the time you call `findViewById` the view objects are already in memory, and the only reason you do so is to get a reference to that particular object (either to change it or get data out of it). – Cristian May 04 '17 at 11:42
-
[This](https://developer.android.com/guide/topics/ui/overview.html) may help. Basically, what @Cristian just said. – orodbhen May 25 '17 at 11:11
-
Drawing means generating a sequence of OpenGL commands. It happens in `onDraw()`. Rendering means executing these commands. Inflation has nothing to do with any of this. It's just a construction of the view hierarchy in the java heap, reading the XML attributes and initializing their respective object members. – WindRider Dec 02 '17 at 15:28
"Inflating" a view means taking the layout XML and parsing it to create the view and viewgroup objects from the elements and their attributes specified within, and then adding the hierarchy of those views and viewgroups to the parent ViewGroup. When you call setContentView(), it attaches the views it creates from reading the XML to the activity. You can also use LayoutInflater to add views to another ViewGroup, which can be a useful tool in a lot of circumstances.
-
23
-
4I'm still a bit confused by the "inflation" terminology myself. (If I could only stop picturing air going into something). But maybe this will help: I just ran the unzip program at an Ubuntu terminal and noticed the terminology: "inflating:X/Y/z.xml." I suppose it's really the same concept. – Ben Ogorek Mar 16 '14 at 23:06
-
7"Inflating" in this case means that the framework is taking the layout description from the XML and populating the view hierarchy with actual View objects. So, it's inflating in the sense of filling out the layout. – jjb Mar 18 '14 at 20:11
-
1@FranciscoCorrales - Sure. The most common thing you'd use this kind of thing for is for something like a [ListAdapter](http://developer.android.com/reference/android/widget/ListAdapter.html), where you want to populate a row. I've used it to populate things like LinearLayouts, iterating over some data object in a way that didn't suggest the ListAdapter approach. – jjb May 09 '14 at 20:27
Inflating is the process of adding a view (.xml) to activity on runtime. When we create a listView we inflate each of its items dynamically. If we want to create a ViewGroup with multiple views like buttons and textview, we can create it like so:
Button but = new Button();
but.setText ="button text";
but.background ...
but.leftDrawable.. and so on...
TextView txt = new TextView();
txt.setText ="button text";
txt.background ... and so on...
Then we have to create a layout where we can add above views:
RelativeLayout rel = new RelativeLayout();
rel.addView(but);
And now if we want to add a button in the right-corner and a textview on the bottom, we have to do a lot of work. First by instantiating the view properties and then applying multiple constraints. This is time consuming.
Android makes it easy for us to create a simple .xml and design its style and attributes in xml and then simply inflate it wherever we need it without the pain of setting constraints programatically.
LayoutInflater inflater =
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View menuLayout = inflater.inflate(R.layout.your_menu_layout, mainLayout, true);
//now add menuLayout to wherever you want to add like
(RelativeLayout)findViewById(R.id.relative).addView(menuLayout);

- 177
- 3
- 3
- 14

- 33,936
- 20
- 234
- 300
-
2I like how you described what inflation actually is, by showing how you would do it manually. – Daniel Apr 26 '17 at 17:08
-
1
-
See detail answer https://stackoverflow.com/questions/12567578/what-does-the-layoutinflater-attachtoroot-parameter-mean – Zar E Ahmer Aug 15 '18 at 05:16
A layman definition for inflation might be to convert the XML code to Java code. Just a way to understand, e.g., if we have a tag in XML, OS has to create a corresponding Java object in memory, so inflatter reads the XMLtags, and creates the corresponding objects in Java.

- 141
- 2
- 1
I think here "inflating a view" means fetching the layout.xml file drawing a view specified in that xml file and POPULATING ( = inflating ) the parent viewGroup with the created View.

- 421
- 5
- 16
Because we make UI into XML but view objects is what we display so we somehow need to convert xml into view objects so inflating means we are converting xml into view objects so that it can be displayed, for this we need a service called layout inflator service and give it an xml and it will be convert for you.

- 6,047
- 13
- 57
- 100
In the iOS UIKit universe, this means getting the reference to the .Xib (which is XML, just like android) file and adding it to the current ViewController's view hierarchy.

- 594
- 1
- 9
- 14