"Avoid passing null as the view root" warning when inflating view for use by AlertDialog
Use this code to inflate the dialog view without a warning:
View.inflate(context, R.layout.dialog_edit, null);
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:
- Suppress the warning using an @Suppress
- Inflate the View using View's inflate method. This is just a wrapper around a LayoutInflater, and mostly just obfuscates the problem.
Inflate the View using LayoutInflater's full method:In older versions of Android Lint, this removed the warning. This is no longer the case in post 1.0 versions of Android Studio.inflate(int resource, ViewGroup root, boolean attachToRoot)
. SetattachToRoot
tofalse
.This tells the inflater that the parent is not available.
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.
Casting null as ViewGroup resolved the warning:
View dialogView = li.inflate(R.layout.input_layout,(ViewGroup)null);
where li
is the LayoutInflater's
object.