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

ID : 10127

viewed : 15

Tags : androidandroid-inflateandroid

Top 5 Answer for android - What does it mean to inflate a view from an xml file?

vote vote

92

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:

  1. Get an instance of the LayoutInflater
  2. Specify the XML to inflate
  3. Use the returned View
  4. 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 
vote vote

82

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

vote vote

73

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

65

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.

vote vote

52

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.

Top 3 video Explaining android - What does it mean to inflate a view from an xml file?

Related QUESTION?