Search code examples
javaandroidmenupopupmenu

PopupMenu Item Icons


I have a problem with my app. I need to show a PopupMenu and I need every item in this menu to contain text and an image. The images should be displayed to the left of the text. But it is not showing, any suggestions how to make it happen?

My code so far below;

public void showMenu(Button button) {
    PopupMenu popupMenu = new PopupMenu(this, button);
    popupMenu.getMenuInflater().inflate(R.menu.config_menu, popupMenu.getMenu());
    popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {

            switch (menuItem.getItemId()) {

And my options menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:id="@+id/dial"
    android:icon="@drawable/phone"
    android:title="@string/dialText"/>

<item
    android:id="@+id/GPS"
    android:icon="@drawable/gps"
    android:title="@string/gpsText"/>

<item
    android:id="@+id/Record"
    android:icon="@drawable/record"
    android:title="@string/recordText"/>

<item
    android:id="@+id/notActive"
    android:title="@string/not_active"/>

This is what I see (red boxes added to show where the images should go);

**enter image description here**


Solution

  • Unfortunately PopupMenu does not support icons by default.

    Alternatives include creating your own class which extends PopupMenu or more simply switching to using an ActionBar. There is a GitHub repository for an Android Compatibility popup menu with icons, which might be helpful.

    Of the three options I would suggest going with the ActionBar because android seem to be heavily pushing the use of the ActionBar as best practice over the traditional header menus in their guides.

    I hope this helps.