androidviewmenumenuitemquickaction

Android : Get view Reference to a Menu Item


I plan to use quick actions UI pattern in my application. Android Quick Actions UI Pattern . The quick action window needs a pivot view to stick to.

    quickAction.show(View pivotView);

I intend to use quick action for the menu Item, I can get access to the item that is clicked. But the problem is i need to reference a view from the menu item so that i can pass it to the quick action.

How can i get reference to a view in the menuItem that is selected.


Solution

  • You can achieve this by providing your menu item with an actionViewClass property in xml and then you will be able to get the pivot view u wanted. The code would be something like this

    <item
        android:id="@+id/menu_find"
        android:showAsAction="ifRoom"
        android:actionViewClass="android.widget.ImageButton"
        />
    

    In your OnCreateOptionsMenu do this

    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu_search, menu);
        locButton = (ImageButton) menu.findItem(R.id.menu_find).getActionView();
        locButton.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                createPopup();
                mQuickAction.show(v);
            }
        });
        return true;
    }