"Avoid passing null as the view root" warning when inflating view for use by AlertDialog

AndroidAndroid AlertdialogLayout Inflater

Android Problem Overview


I get the lint warning, Avoid passing null as the view root when inflating views with null as parent, like:

LayoutInflater.from(context).inflate(R.layout.dialog_edit, null);

However, the view is to be used as the content of an AlertDialog, using setView on AlertDialog.Builder, so I don't know what should be passed as the parent.

What do you think the parent should be in this case?

Android Solutions


Solution 1 - Android

Use this code to inflate the dialog view without a warning:

View.inflate(context, R.layout.dialog_edit, null);

Solution 2 - Android

The short story is that when you are inflating a view for a dialog, parent should be null, since it is not known at View inflation time. In this case, you have three basic solutions to avoid the warning:

  1. Suppress the warning using an @Suppress
  2. Inflate the View using View's inflate method. This is just a wrapper around a LayoutInflater, and mostly just obfuscates the problem.
  3. Inflate the View using LayoutInflater's full method: inflate(int resource, ViewGroup root, boolean attachToRoot). Set attachToRoot to false.This tells the inflater that the parent is not available. In older versions of Android Lint, this removed the warning. This is no longer the case in post 1.0 versions of Android Studio.

Check out http://www.doubleencore.com/2013/05/layout-inflation-as-intended/ for a great discussion of this issue, specifically the "Every Rule Has an Exception" section at the end.

Solution 3 - Android

You should use AlertDialog.Builder.setView(your_layout_id), so you don't need to inflate it.

Use AlertDialog.findViewById(your_view_id) after creating the dialog.

Use (AlertDialog) dialogInterface to get the dialog inside the OnClickListener and then dialog.findViewById(your_view_id).

Solution 4 - Android

Casting null as ViewGroup resolved the warning:

View dialogView = li.inflate(R.layout.input_layout,(ViewGroup)null);

where li is the LayoutInflater's object.

Solution 5 - Android

You don't need to specify a parent for a dialog.

Suppress this using @SuppressLint("InflateParams") at the top of the override.

Solution 6 - Android

When you really don't have any parent (for example creating view for AlertDialog), you have no other choice than passing null. So do this to avoid warning:

final ViewGroup nullParent = null;
convertView = infalInflater.inflate(R.layout.list_item, nullParent);

Solution 7 - Android

According to https://developer.android.com/guide/topics/ui/dialogs

>Inflate and set the layout for the dialog
>Pass null as the parent view because its going in the dialog layout

therefore, for creating AlertDialog, I use @SuppressLint("InflateParams")

LayoutInflater inflater = requireActivity().getLayoutInflater();
@SuppressLint("InflateParams")
View view = inflater.inflate(R.layout.layout_dialog, null);
builder.setView(view);

Solution 8 - Android

  1. The AlertDialog is as far as I know the only case were you can safely use null instead of a parent view. In this case you can suppress the warning by using:

    @SuppressLint("InflateParams")

  2. In general you should never use SupressLint or one of the workarounds mentioned in the other answers to get rid of the warning. The parent view is necessary to evaluate the Layout Params which are declared in the root element of the View being inflated. That means if you use null instead of the parent view, all Layout Params in the root element will be ignored and replaced by default Layout Params. Most of the time it will be fine, but in some cases it will result in a really hard-to-find bug.

Solution 9 - Android

Android's docs (AlertDialog) says:

If you want to display a more complex view, look up the FrameLayout called "custom" and add your view to it:

FrameLayout fl = findViewById(android.R.id.custom);
fl.addView(myView, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));

Therefore, we can use fl as parent in our case:

FrameLayout fl = findViewById(android.R.id.custom);
View view = LayoutInflater.from(context).inflate(R.layout.custom_dialog, fl, false);

It works, but I'm not sure that it's efficient or not

Solution 10 - Android

Instead of doing

view = inflater.inflate(R.layout.list_item, null);

do

view = inflater.inflate(R.layout.list_item, parent, false);

It will inflate it with the given parent, but won't attach it to the parent.

Many thanks to Coeffect (link to his post)

Solution 11 - Android

From the documentation of View.inflate(), it says

Inflate a view from an XML resource. This convenience method wraps the LayoutInflater class, which provides a full range of options for view inflation.

  @param context The Context object for your activity or application.
  @param resource The resource ID to inflate
  @param root A view group that will be the parent.  Used to properly inflate the  layout_* parameters.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRandy Sugianto 'Yuku'View Question on Stackoverflow
Solution 1 - AndroidEdward BreyView Answer on Stackoverflow
Solution 2 - AndroidemersssoView Answer on Stackoverflow
Solution 3 - AndroidJeffrey ChenView Answer on Stackoverflow
Solution 4 - AndroidSVL NarasimhamView Answer on Stackoverflow
Solution 5 - Androidkjdion84View Answer on Stackoverflow
Solution 6 - AndroidMousaView Answer on Stackoverflow
Solution 7 - AndroidCorView Answer on Stackoverflow
Solution 8 - AndroidApfelsaftView Answer on Stackoverflow
Solution 9 - AndroidAmitView Answer on Stackoverflow
Solution 10 - AndroidChristianView Answer on Stackoverflow
Solution 11 - AndroidDivya GuptaView Answer on Stackoverflow