Working of LayoutInflater | Android

pranit sawant
4 min readJun 12, 2017

--

The LayoutInflater takes XML file as an input and builds View objects from it. Fair enough we understood what LayoutInflater do.

Let’s see what LayoutInflater.inflate() document says and specially about 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.

The general idea is this: If attachToRoot is set to true, then the layout file specified in the first parameter is inflated and attached to the ViewGroup specified in the second parameter.

When attachToRoot is false, the layout file from the first parameter is inflated and returned as a View. Means root is only used to create the correct subclass of LayoutParams for the root view(of first parameter) in the XML.

Passing in true for attachToRoot results in a layout file’s inflated View being added to the ViewGroup right on the spot. Passing in false for attachToRoot means that the View created from the layout file will get added to the ViewGroup in some other way.

Let’s break it down in two scenarios:

break it down

1. attachToRoot Set to True

  • Imagine we specified a button in an XML layout file with its layout width and layout height set to match_parent.
<Button xmlns:android=”http://schemas.android.com/apk/res/android"
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”@string/action_attach_to_root_true”
android:id=”@+id/button_ok”>
</Button>
  • We now want to programmatically add this Button to a LinearLayout inside of a Activity. In our LinearLayout member variable parent, we can simply add the button with the following:
inflater.inflate(R.layout.view_button, mLinearLayout, true);
  • We specified that we want to inflate the Button from its layout resource file; we then tell the LayoutInflater that we want to attach it to parent. Our layout parameters are honored because we know the Button gets added to a LinearLayout. The Button’s layout params type should be LinearLayout.LayoutParams.

2. attachToRoot Set to False

  • Let’s take a look at when you would want to set attachToRoot to false. In this scenario, the View specified in the first parameter of inflate() is not attached to the ViewGroup in the second parameter at this point in time. Recall our Button example from earlier, where we want to attach a custom Button from a layout file to parent. We can still attach our Button to parent by passing in false for attachToRoot — we just manually add it ourselves afterward.
Button btnAttachToRootFalse = (Button) inflater.inflate(R.layout.view_button,parent,false);
parent.addView(btnAttachToRootFalse);
  • The false attachToRoot example requires a bit more work when we manually add the View to a ViewGroup. Adding our Button to our LinearLayout was more convenient with one line of code when attachToRoot was true.
  • Let’s look at some scenarios that absolutely require attachToRoot to be false. RecyclerView’s children should be inflated with attachToRoot passed in as false. The child views are inflated in onCreateViewHolder(). RecyclerViews, not us, are responsible for determining when to inflate and present its child Views. The attachToRoot parameter should be false anytime we are not responsible for adding a View to a ViewGroup.
  • There are a few scenarios in which you will not have a root ViewGroup to pass into inflate(). When creating a custom View for an AlertDialog, you do not yet have access to its parent.
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
LayoutInflater inflater = getActivity().getLayoutInflater();
View rootView = inflater.inflate(R.layout.fragment_hello_dialog,null,false);builder.setView(rootView);
builder.setTitle("Alert");
builder.setPositiveButton("Ok", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
return builder.create();
  • In this case, it is okay to pass in null for the root ViewGroup. It turns out that the AlertDialog would override any LayoutParams to match_parent anyway. However, the general rule of thumb is to pass in the parent if you’re able to do so.

So to recap:

  1. If you have a parent to pass into the root ViewGroup parameter, do so.
  2. Try to avoid passing in null for the root ViewGroup.
  3. Pass in false for the attachToRoot parameter when we are not the ones responsible for attaching our layout file’s View to its root ViewGroup.
  4. Do not pass in true for a View that has already been attached to a ViewGroup.
  5. Custom Views are a good use case to pass in true for attachToRoot.

Special thanks to Sean Farrel, about his wonderful article on LayoutInflater here I tried to summarize in few words.

Happy coding and writing about coding!
If you found this helpful, click the 💚 below. Follow me for more articles on technology.

--

--