Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 384
Yashwanth Kumar Avatar asked Dec 23 '11 09:12

Yashwanth Kumar


People also ask

How do I get menu item view?

If you have access to and use AppCompat's Toolbar there is a way. It's not the most efficient way, but it's the easiest way I've found to access the menu item's view. public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { Toolbar toolbar = (Toolbar) view. findViewById(R.

How do I access menu items on Android?

Here's a quick example of how to access an Android MenuItem in a Java Activity or Fragment class (i.e., in your Java code). you can access the MenuItem with the id menuItemPinQuote like this in your Android/Java code: public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) { menuInflater. inflate(R.

What is menu item in Android?

Android Option Menus are the primary menus of android. They can be used for settings, search, delete item etc. When and how this item should appear as an action item in the app bar is decided by the Show Action attribute.

What is collapseActionView?

The collapseActionView flag indicates how to display the widget when the user is not interacting with it: If the widget is on the app bar, the app should display the widget as an icon. If the widget is in the overflow menu, the app should display the widget as a menu item.


2 Answers

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; } 
like image 196
ASH Avatar answered Sep 21 '22 09:09

ASH


Old question, but I ran into some issues with the actionViewClass attribute. For anyone who runs into this later...

Calling findViewById(R.id.mnu_item) in onOptionsItemSelected will return a View anchor.

QuickActions on the MenuItems aren't good design, but I found that they are the simplest way to implement submenus with custom backgrounds.

like image 28
loadedion Avatar answered Sep 17 '22 09:09

loadedion