Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have an ImageView that I attach to a MenuItem as its ActionView (the item appears in the ActionBar). The layout for this view comes from XML. I'm inflating it like so:

ImageView actionView = (ImageView) layoutInflater.inflate(    R.layout.action_view_layout, null); 

This appears to work fine. However; passing null for root in the call to inflate() makes Lint yell at me:

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

I can seemingly manage without a root in my specific case, but I'd rather have the code be as correct as possible. The problem is, I'm not sure which View should be used as the root here. This answer says it should be "the widget that is surrounding the view objects that you want to inflate." But what does that mean here? The one for the action bar? The activity? Something else entirely?


Update: Reading the answers has made me suspect me the right thing to do is:

  1. Get the ActionBar View corresponding to the MenuItem
  2. Get its root
  3. Cast the root to a ViewGroup
  4. Pass the result to the inflater

This seems to work. Can anyone confirm or deny whether this is what should be done?

like image 774
dlf Avatar asked May 24 '14 14:05

dlf


People also ask

What is root in inflate Android?

The first parameter points to the layout resource you want to inflate. The second parameter is the root view of the hierarchy you are inflating the resource to attach to. When the third parameter is present, it governs whether or not the inflated view is attached to the supplied root after inflation.

What does attach to root mean?

attachToRoot: attaches the views to their parent (includes them in the parent hierarchy), so any touch event that the views recieve will also be transfered to parent view. Now it's upto the parent whether it wants to entertain those events or ignore them.

Why attach to root is false?

You should never pass attachToRoot as true when you are not responsible for adding the child view to parent. if you pass the third parameter true you will get IllegalStateException because of this guy. Since you have already added the child fragment in onCreateView() by mistake.

What is inflating a layout?

"Inflating" a view means taking the layout XML and parsing it to create the view and viewgroup objects from the elements and their attributes specified within, and then adding the hierarchy of those views and viewgroups to the parent ViewGroup.


1 Answers

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; } 
like image 85
Damian Petla Avatar answered Oct 12 '22 05:10

Damian Petla