2

I want to add icon with menu title in android, but it not display with title. Below is my menu.xml code

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">

<item android:id="@+id/rate_app"
    android:title="Rate App"
    android:orderInCategory="100"
    app:showAsAction="never"
    app:icon="@drawable/ic_star_half_24dp"/>

I also used android:icon = "@drawable/ic_star_half_24dp", but it didn't display with menu title

shailesh ojha
  • 247
  • 4
  • 15

3 Answers3

3

If your intended behaviour is to show icon on toolbar

app:showAsAction="never" should be changed to ifRoom or always.

If you want to show icon on overflow menu, then you need to customize the menu item and use popup window instead.

User_1191
  • 981
  • 2
  • 8
  • 24
am110787
  • 316
  • 1
  • 2
  • 9
1

try this,

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

 <item
    android:id="@+id/A"
    android:icon="@drawable/ic_a"
    android:title="Name A"
    app:showAsAction="always"/>
 <item
    android:id="@+id/B"
    android:icon="@drawable/ic_b"
    android:title="Name B"
    app:showAsAction="always"/>

      <item
    android:id="@+id/a_More"
    android:icon="@drawable/ic_menu_overflow"
    app:showAsAction="always"
    android:title="More">
    <menu>

        <item 
            android:id="@+id/C"
            android:icon="@drawable/ic_c"
            android:title="Name C"
            app:showAsAction="never" />


        <item 
            android:id="@+id/D"
            android:title="Name D"
            app:showAsAction="never" />

        <item 
            android:id="@+id/E"
            android:icon="@drawable/icon_3"
            android:title="Name E"
            app:showAsAction="never" />    


       </menu>
     </item>
  </menu>
Rgv
  • 504
  • 5
  • 23
0
If you are using popup menu just copy the below code and run it, you will get icons in popupmenu

PopupMenu popup = new PopupMenu(getApplicationContext(), view);

try {
    Field[] fields = popup.getClass().getDeclaredFields();
    for (Field field : fields) {
        if ("mPopup".equals(field.getName())) {
            field.setAccessible(true);
            Object menuPopupHelper = field.get(popup);
            Class<?> classPopupHelper = Class.forName(menuPopupHelper
                    .getClass().getName());
            Method setForceIcons = classPopupHelper.getMethod(
                    "setForceShowIcon", boolean.class);
            setForceIcons.invoke(menuPopupHelper, true);
            break;
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}

    popup.getMenuInflater()
                    .inflate(R.menu.publisher, popup.getMenu());

                    //registering popup with OnMenuItemClickListener
                    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                        public boolean onMenuItemClick(MenuItem item) {

                            switch (item.getItemId()) {
                            case R.id.menu:
                                //your function
                                return true;
                                                        default:
                                break;
                            }
                            return false;
                        }
                    });
                    popup.show();
Somasundaram NP
  • 1,018
  • 1
  • 12
  • 36