Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show popup menu on `ActionBar` item click

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; } 
like image 761
pcu Avatar asked Feb 06 '13 12:02

pcu


People also ask

How do I show the pop up menu on Android?

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.

How do I hide a menu item in the Actionbar?

You can set a flag/condition. Call invalidateOptionsMenu() when you want to hide the option. This will call onCreateOptionsMenu() .

Which method is used to create menu items on Actionbar?

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.

How do I replace my action bar toolbar?

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.

How do I Hide and show a menu item in Android Actionbar?

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

How to anchor popupmenu to actionitem in Actionbar in 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.

What is Android popup menu and how to use it?

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.

How do I add a pop up menu in Windows 10?

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.


1 Answers

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.

like image 188
pcu Avatar answered Sep 21 '22 21:09

pcu