Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Popup menu with icon on Android

My menu xml code menu.xml:

<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Single menu item 
     Set id, icon and Title for each menu item
-->

 <item android:id="@+id/back"
      android:icon="@drawable/back1"
        android:showAsAction="never"
      android:title="Back" />

<item android:id="@+id/My_Profile"
      android:icon="@drawable/myprofile"
       android:showAsAction="never"
      android:title="My Profile" />

<item android:id="@+id/Job_Alert"
      android:icon="@drawable/jobalert4"
       android:showAsAction="never"
      android:title="Job Alert !" />

<item android:id="@+id/saved_job"
      android:icon="@drawable/jobapplied"
      android:title="Saved Jobs"
       />


<item android:id="@+id/Logout"
      android:icon="@drawable/logout"
      android:title="Logout" /> 
</menu>

I am calling menu xml like this

     PopupMenu popup = new PopupMenu(getBaseContext(), v);
 popup.getMenuInflater().inflate(R.menu.menu,  popup.getMenu());
     popup.show();

But it does not show the icon.

How can I set the icon on the popup menu?

like image 210
Kuldeep Avatar asked Dec 30 '13 09:12

Kuldeep


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.

What displays a pop up menu?

Pop-up menus appear over the application in a vertical orientation while the user is clicking an item, and then they disappear from the screen. You can programmatically invoke a pop-up menu, or you can arrange for a right-mouse click to automatically invoke a pop-up menu tied to a window or a control.


3 Answers

You can create popup menu with icon using the MenuBuilder and MenuPopupHelper.

MenuBuilder menuBuilder =new MenuBuilder(this);
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.menu, menuBuilder);
MenuPopupHelper optionsMenu = new MenuPopupHelper(this, menuBuilder, view);
optionsMenu.setForceShowIcon(true);

// Set Item Click Listener
menuBuilder.setCallback(new MenuBuilder.Callback() {
    @Override
    public boolean onMenuItemSelected(MenuBuilder menu, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.opt1: // Handle option1 Click
                return true;
            case R.id.opt2: // Handle option2 Click
                return true;
            default:
                return false;
        }
    }

    @Override
    public void onMenuModeChange(MenuBuilder menu) {}
});

optionsMenu.show();

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/opt1"
        android:icon="@mipmap/ic_launcher"
        android:title="option 1" />
    <item
        android:id="@+id/opt2"
        android:icon="@mipmap/ic_launcher"
        android:title="option 2" />
</menu>

enter image description here

like image 77
Ajay Sivan Avatar answered Oct 20 '22 10:10

Ajay Sivan


You can enable icons for popup menu by using Java reflection to call a hidden method as below:

public static void setForceShowIcon(PopupMenu popupMenu) {
    try {
        Field[] fields = popupMenu.getClass().getDeclaredFields();
        for (Field field : fields) {
            if ("mPopup".equals(field.getName())) {
                field.setAccessible(true);
                Object menuPopupHelper = field.get(popupMenu);
                Class<?> classPopupHelper = Class.forName(menuPopupHelper
                        .getClass().getName());
                Method setForceIcons = classPopupHelper.getMethod(
                        "setForceShowIcon", boolean.class);
                setForceIcons.invoke(menuPopupHelper, true);
                break;
            }
        }
    } catch (Throwable e) {
        e.printStackTrace();
    }
}
like image 39
Bao Le Avatar answered Oct 20 '22 10:10

Bao Le


before popupMenu.show(); use

try {
                    Field mFieldPopup=popupMenu.getClass().getDeclaredField("mPopup");
                    mFieldPopup.setAccessible(true);
                    MenuPopupHelper mPopup = (MenuPopupHelper) mFieldPopup.get(popupMenu);
                    mPopup.setForceShowIcon(true);
                } catch (Exception e) {

                }
like image 38
Gil Snovsky Avatar answered Oct 20 '22 08:10

Gil Snovsky