0

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.

Robin Kanters
  • 5,018
  • 2
  • 20
  • 36
Vipul Singh
  • 393
  • 9
  • 26

2 Answers2

3

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!

Jayanth
  • 5,954
  • 3
  • 21
  • 38
0

try like this

PopupMenu popup = new PopupMenu(MainActivity.this, button1);
 popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
 popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                startActivity(new Intent(MainActivity.this, SecounActivity.class));
                return true;
            }
        });
popup.show();
Hemanth S
  • 669
  • 6
  • 16
  • Can you explain why this is different and why this works? It seems the same as OP's code to me – Robin Kanters Feb 11 '17 at 09:17
  • Is it working or not, `MenuInflater inflater = popup.getMenuInflater(); inflater.inflate(R.menu.emailmenu, popup.getMenu());` its like your creating new object for it. im not sure 100% but this could be the problem – Hemanth S Feb 11 '17 at 10:31
  • no my code was working but as I did not added title so names were not coming. so this was my first concern, secondly I wanted to start an activity based on popup option selected. I have marked answer as correct as It solved my problem. – Vipul Singh Feb 13 '17 at 08:55