user3077648
user3077648

Reputation:

PopUp menu with icons

I'm new on android. I can't add simple icons to the pop up menu. I added the icons to the xml file but I can't see them.

This is my menu - pop_up_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/action_email"
        android:icon="@mipmap/email"
        android:title="E-Mail"
        />
    <item
        android:id="@+id/action_messenger"
        android:icon="@mipmap/messenger"
        android:title="Messenger"
        />
    <item
        android:id="@+id/action_skype"
        android:icon="@mipmap/skype"
        android:title="Skype"
        />
    <item
        android:id="@+id/action_whatsapp"
        android:icon="@mipmap/whatsapp"
        android:title="Whatsapp"
        />
</menu>

I have this OnClickListener that call the showPopUpMenu function after clicking on the moreActionsButton

moreActionsButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        showPopUpMenu(us);
    }
});

This is the showPopUpMenu that creates the menu

public void showPopUpMenu(final User user) {
    View menuItemView = getView().findViewById(R.id.groupLeave);
    PopupMenu popUpMenu = new PopupMenu(getActivity(), menuItemView);
    popUpMenu.getMenuInflater().inflate(R.menu.pop_up_menu, popUpMenu.getMenu());
    popUpMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.action_messenger:
                    onMessengerClick(user);
                    break;

                case R.id.action_skype:
                    onSkypeClick(user);
                    break;

                case R.id.action_whatsapp:
                    onWhatsappClick(user);
                    break;

                case R.id.action_email:
                    onEmailClick(user);
                    break;

                default:
                    break;

            }
            return true;
        }
    });
    popUpMenu.show();
}

Upvotes: 2

Views: 4132

Answers (1)

Gauthier
Gauthier

Reputation: 4978

By default the PopupMenu doesn't show Icons next to your title.

What you need to do is either create your own PopupMenu implementation and force the use of icons by overriding the default constructor :

 public CustomPopupMenu(Context context, View anchor) {
       ...
       mPopup.setForceShowIcon(true);
    }

OR

Just before showing your PopupMenu, use reflection to invoke the desired method

        //... initialization of your PopupMenu
        Object menuHelper;
        Class[] argTypes;
        try {
            Field fMenuHelper = PopupMenu.class.getDeclaredField("mPopup");
            fMenuHelper.setAccessible(true);
            menuHelper = fMenuHelper.get(popupMenu);
            argTypes = new Class[]{boolean.class};
            menuHelper.getClass().getDeclaredMethod("setForceShowIcon", argTypes).invoke(menuHelper, true);
        } catch (Exception e) {
            // Possible exceptions are NoSuchMethodError and NoSuchFieldError
            //
            // In either case, an exception indicates something is wrong with the reflection code, or the
            // structure of the PopupMenu class or its dependencies has changed.
            //
            // These exceptions should never happen since we're shipping the AppCompat library in our own apk,
            // but in the case that they do, we simply can't force icons to display, so log the error and
            // show the menu normally.
        }
        popUpMenu.show();

Upvotes: 6

Related Questions