Understanding Android Service: LayoutInflater

Tram Ho

In this article I want to share with you about LayoutInflater, one of the important services of Android.

As you know, to draw a view, we have 2 ways: you can create directly by code:

like this. When you want to access the views to do something, you just need to directly call the created view object to have access to it, it’s convenient. The downside is that when you want to customize it, it will be very laborious and difficult. Or method 2 is to draw with XML file:

Most of us find drawing on XML will be nicer and easy to customize view as desired, but in return, when you want to access the view in XML (to set listener events for example), many people do not know how to do it. how. That’s the job of LayoutInflater.

According to Android Developer:

Instantiates a layout XML file into its corresponding View objects.

It can be understood that LayoutInflater is a component that helps you convert XML file layout into View Object in Android. You usually use it in fragment’s onCreateView method or when custom View, when you need to convert XML to POJO.

1. How to instantiate the LayoutInflater object

It is never used directly. Instead, use Activity.getLayoutInflater() or Context#getSystemService to retrieve a standard LayoutInflater instance that is already hooked up to the current context and correctly configured for the device you are running on.

As can be seen we will not use the Constructor of the LayoutInflater class directly. There are two main ways to retrieve that object:

* Like every other Service of Android

This is a bit verbose, so I don’t usually use it.

* Use static methods in LayoutInflater

This is the way I use it, because it’s short. In essence it is the same way only

2. Use the inflate () method

Okay, already have layoutInflater object, now how to parse the XML code into Java object here? LayoutInflater’s job is to read the XML layout file and convert its properties to a View in Java code, using the inflate () method. We have 2 inflate methods with different signatures:

The third parameter attachToRoot you do not need to understand, look at the first 2 parameters: resource is the id of the xml file you need to convert, and the parent is the ViewGroup where the xml layout file (the first parameter) can be embedded. in, LayoutInflater will convert the xml layout file to View and use the properties that match ViewGroup parent.

The inflate method returns the view converted from the XML.

Pretty easy to understand, isn’t it?

3. attachToRoot ? true or false

This third property, I think many people will find it quite chaotic not knowing when to set it to true, not knowing when to set it to true.

The description of the google side is not very clear:

attachToRoot boolean: Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

Here is the code that layoutInflater uses:

Basically, attachToRoot if set to true, then the view you just inflate will be added to the (ViewGroup) parent immediately, otherwise it (can) be added later, somehow depending on the case. you use. Imagine you have 1 view:

Now I want to programatically add this Button to LinearLayout inside Fragment or Activity. If my LinearLayout was already a member variable, mLinearLayout, I could simply add the following:

or

by default the attachToRoot will have the value true. Another suitable use of setting true for attachmentToRoot is when we use Custom Views.

In case of attachToRoot set False

Let’s see when you want to set attachToRoot to false. The specified View is then not attached to the View Group in inflate () ‘s second parameter at that moment. Looking back at the Button case I just set, you do like this not wrong

Here you can completely add views manually, but there are some cases where you must not add views manually.

A RecyclerView item view will not be set attachToRoot true, or set False and call addView manually.

If you manually add it there will be problems. Try it

RecyclerView will take responsibility for the add on my behalf, since it has to handle more “under the hood”. Currently we do not need to understand that. Some guys will have to set the attachToRoot false like: RecyclerView, Dialog, Fragment …, you can learn more.

Hope my article will help you who are still in trouble. Best regard, good bye !!

Share the news now