Search code examples
androidandroid-optionsmenuandroid-popupwindow

Android popup menu is not loading menu


In my android app I have a menu option which works when the device has dedicated hardware menu option. for another device I don't have dedicated Menu button so I tried to add a button and on click of that a popup will display the menu, The popup is working but not displaying the options, and further to that how to work on the selected popup option.

My button layout is as follows:

<ImageButton
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/ic_overflow_holo_dark"
    android:contentDescription="@string/descr_overflow_button"
    android:onClick="showPopup" />

This is my code to show the popup:

public boolean showPopup(View v) {
    PopupMenu popup = new PopupMenu(this, v);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.emailmenu, popup.getMenu());
    popup.show();
    return true;
} 

And this my option code:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/bluebutton" android:icon="@drawable/icon_blue_btn"
        android:title="" />
    <item android:id="@+id/zephyr" android:icon="@drawable/icon_zephyrme"
        android:title="" />
    <item android:id="@+id/skype" android:icon="@drawable/icon_skype"
        android:title="" />
</menu>

I all ready have this onCreateOptionsMenu() for triggering the option from dedicated menu key. How to get it to work from popup.


Solution

  • If you want to show Icons instead of Title, then create your PopupMenu like below

    The method setForceShowIcon(menu); will force the PopMenu to show icons. you can have text with icons too.

    private void showPopupMenu(){
        PopupMenu menu=new PopupMenu(this,anchorView);
        menu.getMenuInflater().inflate(R.menu.popup_menu,menu.getMenu());
        setForceShowIcon(menu);
        menu.setOnMenuItemClickListener(menuClickListner);
        menu.show();
    }
    
     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();
        }
    }
    //This is Menu click listner
    PopupMenu.OnMenuItemClickListener menuClickListner = new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()){
                case R.id.some_id1:
                    //actions here
                    break;
                case R.id.some_id2:
                    //actions here..
                    break;
                case R.id.some_id3:
                    break;
               }
            return false;
        }
    };
    

    Hope this helps!