Convert XML resources to object views

Introduction

Layout inflation is a term that refers to the process by which an XML resource is parsed and converted to a View object.
Activity has its own setContentView () method with which it inflates its root view:

Example

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.some_view);
}

But converting other layouts to a View object (allowing layout elements to be available in the activity code) can be done in the following ways:

  • Using the inflate () method of the inflater object
  • Using the static inflate () View class method

LayoutInflater.inflate () method

This process consists of two steps:

  • Creating an Inflater Object
  • Calling the inflation () method

Creating an Inflater Object

In the activity

a) within onCreate ()
If we are in activity on the onCreate () method, we can access this object simply because it is given as a parameter of the onCreate () method:

 View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

b) outside the onCreate () method
However, if we are outside the onCreate () method, we need to access it differently using the getLayoutInflater () method:

LayoutInflater inflater = getLayoutInflater();

Outside of activity

a) Via activities
So even if we are out of activity, we can access it by calling the activity:

getActivity().getLayoutInflater();

b) Via context

Using the context.getSystemService (Class) method

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

Example

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:

LayoutInflater inflater = LayoutInflater.from(context);

Calling the inflation () method

Calling the “inflate ()” method converts:

public View inflate (int resource,ViewGroup root,boolean attachToRoot)
  • resource int: Resource ID
  • root ViewGroup: An optional value representing some View group that will be the parent of this view (if the third “attachToRoot” parameter is set to true). This value can be null when calling a 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 inflate parameter and attached to ViewGroup is defined in the second parameter, then the method returns this combined view, with ViewGroup as root. When attachToRoot is false, the layout file from the first parameter is inflated and returned as a display.

Example

@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 the 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

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)

public View inflate (int resource, ViewGroup root)

Static inflate () View class method

The static view of the Inflate object () can also be used for the same purpose. In the background, it also uses a wrap-inflater object whose code looks like this:

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 commonly used when defining custom VIew:

public void init (Context context, AttributeSet attrs) {
    customView = inflate(context, R.layout.view_custom, this);
}

Which would be exactly the same as the following code done through an inflation object:

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 difference is that in this static method, we do not have a three parameter option (which means that the third parameter is always true)

Likes:
15 0
Views:
1842
Article Categories:
PROGRAMMINGTECHNOLOGY

Leave a Reply

Your email address will not be published. Required fields are marked *