Avoid passing null as the view root (when inflating custom layout in AlertDialog)
Asked Answered
W

4

5

Trying to inflate a custom layout for an AlertDialog, but keep getting this waring. I've seen several different solutions to this but don't know which is correct for my scenario. What is the actual correct way of getting rid of this null warning?

Avoid passing null as the view root (need to resolve layout parameters on the inflated layout's root element)

@Override
public void onClick(View v) {
  AlertDialog alertDialog = new 
  AlertDialog.Builder(getActivity()).create();

  LayoutInflater inflater = getActivity().getLayoutInflater();
  View content = inflater.inflate(R.layout.dialog_customd, null);
  alertDialog.setView(content);

  alertDialog.show();
}
Whiffen answered 31/7, 2017 at 15:20 Comment(6)
There is none, AFAIK, other than to use the quick-fix to suppress that Lint warning. While in general the Lint advice is sound, this is one of the cases where I do not think that you have access to the parent container.Exchequer
So considering your response, is using @SuppressLint("InflateParams") View content = inflater.inflate(R.layout.dialog_customd, null); a good idea?Whiffen
Whether it is a "good idea" is up to you. :-)Exchequer
Cool. Thanks for the tip.Whiffen
Do you have any idea whatsoever how to resolve with this question? I've spent weeks trying to solve it but have had no luck.Whiffen
use View content = inflater.inflate(R.layout.dialog_customd, parent, false);Morpheus
H
4

Do it like this:

View content = inflater.inflate(R.layout.dialog_customd, parent, false);
Haploid answered 16/8, 2017 at 17:42 Comment(0)
B
10

You may try to use:

View.inflate(context, R.layout.dialog_customd, null);
Barthelemy answered 13/10, 2017 at 15:51 Comment(0)
H
4

Do it like this:

View content = inflater.inflate(R.layout.dialog_customd, parent, false);
Haploid answered 16/8, 2017 at 17:42 Comment(0)
F
1

Incase someone still faces this issue like myself, @Dmitry's solution works fine -

View view = View.inflate(this, R.layout.dialog_set_height, null);

Below line of code is not required -

LayoutInflater inflater = getActivity().getLayoutInflater();
Freezing answered 17/6, 2020 at 15:54 Comment(0)
C
0

If you use viewbinding

the solution is:

LayoutInflater.from(context).inflate(R.layout.inflatedLayout, binding.root, false) as NativeAdView

So you can use binding.root as root

Condor answered 20/8, 2021 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.