144

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?

7
  • 3
    Pass false instead of null. see this Commented Oct 16, 2014 at 12:54
  • 1
    I suppress the lint warning for the method. I have not yet seen a good reason why I shouldn't pass null. Commented Oct 16, 2014 at 14:09
  • 4
    I understand the layout issue depending on the parent view, but that doesn't really apply for an AlertDialog which essentially floats above the Activity's view hierarchy. That is why you can pass null. There's a reason that you can suppress lint. Lint is supposed to give you warnings for often-missed issues; in this case, it's correct invocation. Commented Oct 17, 2014 at 1:50
  • 1
    For those looking, to do this the exact suppression needed is @SuppressLint("InflateParams"). Commented Jan 28, 2017 at 13:38
  • 1
    @ashutiwari4 I am learning new new things everyday.. beautiful world :I Commented Apr 24, 2017 at 6:51

11 Answers 11

193

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

View.inflate(context, R.layout.dialog_edit, null); 
Sign up to request clarification or add additional context in comments.

11 Comments

@MarianPaździoch Because it doesn't cause a warning. :-) Of course, that begs the question of why the lint code treats View.inflate and LayoutInflator.inflate differently. I've not seen a definitive answer. It may be related to why there are two seemingly equivalent ways to inflate the same view, for which I've also not seen a rationale.
This is wrong. It only suppresses the warning, but the problem is still there. Read possiblemobile.com/2013/05/layout-inflation-as-intended for a better solution.
@JonaChristopherSahnwaldt Could you elaborate? The article says that AlertDialog should have a null parent because it does not have a root view. To me it seems to reinforce that the warning makes sense for views outside a dialog but is not applicable for a dialog view.
@EdwardBrey You are right - I didn't read the article thoroughly. In this particular case, it's OK to use either View.inflate(...null) or @SuppressLint. In general, LayoutInflater.from(...).inflate(..., parent, false) is better. Thanks for pointing this out!
@Bevor That Avoid passing null question is about cases where you have a parent. This question is about AlertDialog, which provides no parent for the view being inflated. When inflating the root view of an AlertDialog, passing null is correct. Lint doesn't account for this particular context, so the warning it gives if you use the function that takes parent a false positive, not an indication of anything error prone. That's why it's valid in this case to use the function that takes no parent and triggers no warning.
|
38

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.

Comments

22

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).

3 Comments

This is the way to go! Just create the dialog and then find your view.
This is a good way to do it, however remember that this method was added in API 21
What if we must store this inflated layout in some variable? For example, I wrote a class A in which there is the method onCreateDialog. In the latter, I wrote as ou said alert_dialog_builder.setView(R.layout.edit_account_dialog);. But I have to store this inflated layout in the A's attribute variable named the_inflated_layout_of_the_dialog.
19

Casting null as ViewGroup resolved the warning:

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

where li is the LayoutInflater's object.

8 Comments

where 'li' is the LayoutInflater object.
This answer is the best, providing you understand WHY you are actually doing this. Short story, as you might have already read, is that AlertDialog does not know its parent at inflate time, and so it's a side effect that lint throws a warning in this precise situation when you actually are doing the right thing.
But it is redundant casting
Then we get another warning "Casting is redundant"!
are you joking?
|
11

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

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

1 Comment

Correct. Even Google passes null in this case: builder.setView(inflater.inflate(R.layout.dialog_signin, null)) developer.android.com/develop/ui/views/components/…
8

Edit:

At the time of writing this answer, I was not aware of Lint suppressions. It's better to suppress Lint instead of tricking the IDE. Add this above the line or method:

@SuppressLint("InflateParams") 

Old answer:

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:

// Java final ViewGroup nullParent = null; convertView = infalInflater.inflate(R.layout.list_item, nullParent); // Kotlin val nullParent: ViewGroup? = null val convertView = layoutInflater.inflate(R.layout.my_dialog, nullParent) 

3 Comments

It produces another warning "value 'nullParent' is always null."
As of AndroidStudio 3.3 and API 28, this is the only solution with works.
@zeeshan Not true. The only "solution" always has been @SuppressLint("InflateParams") if you have no parent.
1

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); 

Comments

0
  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.

Comments

0

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

Comments

-2

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)

1 Comment

It would also be useful to know how to get parent.
-2

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. 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.