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

Leave a Comment