Introduction
“Layout inflation” is a term used to describe the process by which an XML resource is parsed and converted into a View object.
The activity has its own method setContentView() with which it inflates its root view:
Example
|
1 2 3 4 5 |
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.neki_view); } |
But we can convert other layouts into a View object (thereby enabling the layout elements to be available in the activity code) in the following ways:
LayoutInflater.inflate() method
This procedure consists of two steps:
- Creating an Inflater object
- Calling the inflate() method
Creating an Inflater object
In activity
a) within onCreate()
If we are in the activity within the onCreate() method, we can access this object simply because it is given as a parameter of the onCreate() method:
|
1 |
View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) |
b) outside the onCreate() method
However if we are outside the onCreate() method we must access it in a different way using the getLayoutInflater() method:
|
1 |
LayoutInflater inflater = getLayoutInflater(); |
Out of action
a) Via activity
And even if we are out of the activity we can access it by calling the activity:
|
1 |
getActivity().getLayoutInflater(); |
b) Via context
Using the context.getSystemService(Class)
method
|
1 |
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); |
Example
|
1 2 3 4 5 |
public MyAdapter(Context context, List<MyObject> objects) extends ArrayAdapter { super(context, 1, objects); /* We get the inflator in the constructor */ mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } |
or otherwise using the from() method:
|
1 |
LayoutInflater inflater = LayoutInflater.from(context); |
Calling the inflate() method
Calling the “inflate()” method converts:
|
1 |
public View inflate (int resource,ViewGroup root,boolean attachToRoot) |
Legend:
- resource int: Resource ID
- root ViewGroup: An optional value representing a View group which will be the parent of this view (if the third parameter “attachToRoot” is set to true). This value can be null when calling the function!
- attachToRoot boolean: This parameter defines what will be returned from the method. If attachToRoot is set to true, then the layout file specified in the first parameter is inflated and attached to the ViewGroup defined in the second parameter, and then the method returns this combined view, with the ViewGroup as the root. When attachToRoot is false, the layout file from the first parameter is inflated and returned as a display.
Example
|
1 2 3 4 5 |
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_main, container, false); return rootView; } |
In this example, the third parameter is “false”, so our view is not inserted into the parent view (here container). If we want the view to be inserted into a parent view, we need to set it to “true” or do it later with additional code using the addView() method.
Example
|
1 2 |
Button button = (Button) inflater.inflate(R.layout.custom_button, mLinearLayout, false); mLinearLayout.addView(button); |
NOTE:
There is also a version with two parameters (the third parameter is always true)
|
1 |
public View inflate (int resource, ViewGroup root) |
Static inflate() method of View class
The STATIC method of the View object inflate() can be used for the same purpose. In the background, she also uses a wrapped inflater object whose code looks like this:
|
1 2 3 4 |
public static View inflate(Context context, int resource, ViewGroup root) { LayoutInflater factory = LayoutInflater.from(context); return factory.inflate(resource, root); } |
Example
This static method is most often used when custom VIew:
is defined
|
1 2 3 |
public void init (Context context, AttributeSet attrs) { customView = inflate(context, R.layout.view_custom, this); } |
Which would be exactly equivalent to the following code done via the inflator object:
|
1 2 3 4 |
public void init (Context context, AttributeSet attrs) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); View customView = inflater.inflate(R.layout.view_custom, this, true); } |
One of the differences is that in this static method we don’t have a three-parameter option (that means the third parameter is always true)

