I have an ActionBar
with an action item on it. After clicking on the action item, I want to show a popup menu. I implemented this method, but I want to anchor it to the action item or to the ActionBar
, not to any view from layout. How to get some kind of view to anchor it from MenuItem
?
public boolean onOptionsItemSelected(MenuItem item) { PopupMenu popupMenu = new PopupMenu(this, ??????); // What view goes here? popupMenu.inflate(R.menu.counters_overflow); popupMenu.show(); // ... return true; }
Go to app > res > right-click > New > Android Resource Directory and give Directory name and Resource type as menu. Now, we will create a popup_menu file inside that menu resource directory. Go to app > res > menu > right-click > New > Menu Resource File and create a menu resource file and name it as popup_menu.
You can set a flag/condition. Call invalidateOptionsMenu() when you want to hide the option. This will call onCreateOptionsMenu() .
The Android tooling automatically creates a reference to menu item entries in the R file, so that the menu resource can be accessed. An activity adds entries to the action bar in its onCreateOptionsMenu() method. The showAsAction attribute allows you to define how the action is displayed.
You'll want to add android:fitsSystemWindows="true" to the parent layout of the Toolbar to ensure that the height of the activity is calculated correctly. When using the support library, make sure that you are importing android. support. v7.
This example demonstrates how do I hide and show a menu item in the Android ActionBar. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.java
When you want to anchor popupmenu to ActionItem in ActionBar you need to find view that renders ActionItem. Simple find view with findViewById() where id is same as id of your menu item in xml.
Android Popup Menu: Android Popup Menu displays a list of items in a vertical list which presents to the view that invoked the menu and useful to provide an overflow of actions that related to specific content. So in this article, we are going to discuss the Popup Menu. A PopupMenu displays a Menu in a popup window anchored to a View.
Go to app > res > menu > right-click > New > Menu Resource File and create a menu resource file and name it as popup_menu. In the popup_menu file, we will add menu items. Below is the code snippet for the popup_menu.xml file.
So finally I found solution. When you want to anchor popupmenu to ActionItem in ActionBar you need to find view that renders ActionItem. Simple find view with findViewById() where id is same as id of your menu item in xml.
DISPLAYING POPUP:
public boolean onOptionsItemSelected(MenuItem item) { // ... View menuItemView = findViewById(R.id.menu_overflow); // SAME ID AS MENU ID PopupMenu popupMenu = new PopupMenu(this, menuItemView); popupMenu.inflate(R.menu.counters_overflow); // ... popupMenu.show(); // ... return true; }
MENU:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" > .... <item android:id="@+id/menu_overflow" android:icon="@drawable/ic_overflow" android:showAsAction="ifRoom" android:title="@string/menu_overflow"/> .... </menu>
If menu item is not visible (is in overflow) it does not work. findViewById returns null so you have to check for this situation and anchor to another view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With