Converting an XML resource to a View object (“Layout Inflating”)

Converting an XML resource to a View object (“Layout Inflating”)

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

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:

  1. Use the inflate() method of the inflater object
  2. Using the static inflate() method of the View class

LayoutInflater.inflate() method

This procedure consists of two steps:

  1. Creating an Inflater object
  2. 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:

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:

Out of action

a) Via activity

And even if we are out of the activity we can access it by calling the activity:

b) Via context

Using the context.getSystemService(Class)

method

Example

or otherwise using the from() method:

Calling the inflate() method

Calling the “inflate()” method converts:

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

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

NOTE:
There is also a version with two parameters (the third parameter is always true)

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:

Example

This static method is most often used when custom VIew:

is defined

Which would be exactly equivalent to the following code done via the inflator object:

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)