Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is LayoutInflater and how do I use it properly?

What exactly is a LayoutInflater in Android? What is the intended method to use it? I could find different types of usage but unable to find which one suites in my case.



About the question

I had so many confusions about the proper usage of the inflate() method. When researched on the internet, Most of the results was either wrong or incomplete. Even the official doc is very vague. This post is a sum up of what I could find in different places. I believe this will be helpful to beginners like me

like image 484
Bertram Gilfoyle Avatar asked Dec 14 '22 15:12

Bertram Gilfoyle


1 Answers

What is a LayoutInflater?

LayoutInflater is a class used to create views from a layout resource (xml) file or a node of it (XmlPullParser objects).

These can be a representation of either a single view or a view hierarchy.


Creating LayoutInflater object

To inflate a view, we need a LayoutInflater object. Instead of creating new object, we use one of these methods normally to get an existing object with the context.

  • LayoutInflater#from(Context context)
  • Activity#getLayoutInflater()
  • Context#getSystemService(Class<T> serviceClass)
  • Context#getSystemService(String name)

The first one is most commonly used because of its simplicity.

Here are sample usages of the last two methods.

LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)
LayoutInflater inflater = (LayoutInflater)getSystemService(LayoutInflater.class)


Inflating views

To inflate views, LayoutInflater#inflate() method can be used. It has four forms as listed below. One of the first two methods can be used if the source is a layout resource. Last two methods are used if the source is a layout resource node.

  1. View inflate(int resource, ViewGroup root)

  2. View inflate(int resource, ViewGroup root, boolean attachToRoot)

  3. View inflate(XmlPullParser parser, ViewGroup root)

  4. View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)

root: It is a ViewGroup to which the newly created view hierarchy is about to attached.

attachToRoot: The first and third methods attaches the newly created view hierarchy to the root once it is created. However if you choose manually add it by ViewGroup#addView() or attaching should be taken place at somewhere else, then you can choose the second or last method and set attachToRoot as false.
For instance, inside Fragment’s onCreateView() and when creating a view as RecyclerView’s itemView. You should set attachToRoot as false in these two places because the attaching will be done somewhere else. If we set it as true or use the first or third method in such places, it will throw an error.

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.


Using the result

If attachToRoot is true, the result will be the root view. Otherwise it will be the newly created view hierarchy.

Theoretically all these methods returns the same thing - the root view. However, to us, they are not the same. Are they?


Some common mistakes

It is seen that setting root as null even if it is known. Root can be null if attachToRoot is false. However, it should be given if possible because it is used to create the correct subclass of LayoutParams.

like image 166
Bertram Gilfoyle Avatar answered Dec 27 '22 17:12

Bertram Gilfoyle