Avoid Passing Null as the View Root (Need to Resolve Layout Parameters on the Inflated Layout's Root Element)

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

Instead of doing

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

do

convertView = infalInflater.inflate(R.layout.list_item, parent, false);

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

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

Do it like this:

View content = inflater.inflate(R.layout.dialog_customd, parent, false);

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

No parent found for avoid passing null as the view root

The warning should not apply in this case. The root parameter, when attachToRoot is false, is only used to call its generateDefaultLayoutParams() method and assign the resulting LayoutParams to the inflated view.

In this case, the dialog will overwrite them, so they would be unused anyway.

Please check this article, particularly the section Every Rule Has An Exception.

However, because the result will go into the dialog, which does not
expose its root view (in fact, it doesn’t exist yet), we do not have
access to the eventual parent of the layout, so we cannot use it for
inflation. It turns out, this is irrelevant, because AlertDialog will
erase any LayoutParams on the layout anyway and replace them with
match_parent.

Avoid passing null as the view root with popupwindow

You could just use:

context = popupView.getContext();
inflater.inflate(R.layout.resetpop, new LinearLayout(context), false);

That should solve the warning.

What should I pass for root when inflating a layout to use for a MenuItem's ActionView?

I would simply do it like this:

menuItem.setActionView(R.layout.action_view_layout);

Let Android inflate the view for you.

If you need to do some extra changes on this ImageView call

ImageView imageView = (ImageView) menuItem.getActionView();

Update

In order to cater to your curiosity. That is what folks from Google do under the hood:

public MenuItem setActionView(int resId) {
final Context context = mMenu.getContext();
final LayoutInflater inflater = LayoutInflater.from(context);
setActionView(inflater.inflate(resId, new LinearLayout(context), false));
return this;
}


Related Topics



Leave a reply



Submit