Home » Android » android – What does it mean to inflate a view from an xml file?

android – What does it mean to inflate a view from an xml file?

Posted by: admin March 10, 2020 Leave a comment


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.

How to&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

You can also inflate views explicitly by using the LayoutInflater. In that case you have to:

  1. Get an instance of the LayoutInflater
  2. Specify the XML to inflate
  3. Use the returned View

For instance:

LayoutInflater inflater = LayoutInflater.from(YourActivity.this); // 1
View theInflatedView = inflater.inflate(R.layout.your_layout, null); // 2 and 3


“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.


Inflating is the process of adding a view(.xml) to activity on runtime. When we create a listView we inflate its each item dynamically. If we want to create a ViewGroup with multiple views like buttons and textview . We can create it like

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();


And now if we want to add button on right-corner and textview on the bottom . We have to do a lot of job. First with the view properties and secondly we have to apply multiple constraints. It is a tough and time consuming job.

Android make it easy for us to create a simple .xml and design its style and attributes in xml and just simply inflate it wherever we need it without the confusion of setting constraints and setting it programatically.

LayoutInflater inflater = 
View menuLayout = inflater.inflate(R.layout.your_menu_layout, mainLayout, true);
//now add menuLayout to wherever you want to add like



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.


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.


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.