0

I am developing the overflow menu for latest android version as i am doing this,the text items are being added to the overflow menu but item images are not adding. so please help me to know how to add images to the overflow menu.

I have attached screenshot and Code.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/new_game"
    android:icon="@drawable/ic_new_game"
    android:title="@string/new_game"
    android:showAsAction="ifRoom"/>

enter image description here

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
NarasimhaKolla
  • 791
  • 3
  • 7
  • 14
  • Check this question please: http://stackoverflow.com/questions/27919595/popupmenu-item-icons – Kushan Jul 08 '16 at 06:15

3 Answers3

1

In your menu xml, use the following syntax to nest menu, you will start getting the menu with icons

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

    <item
        android:id="@+id/empty"
        android:icon="@drawable/ic_action_overflow"
        android:orderInCategory="101"
        android:showAsAction="always">
        <menu>
            <item
                android:id="@+id/new_game"
                android:icon="@drawable/ic_new_game"
                android:showAsAction="always|withText"
                android:title="@string/new_game" />
        </menu>
    </item>

</menu>
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
0

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.

@source -> PopupMenu Item Icons

Community
  • 1
  • 1
Kushan
  • 5,855
  • 3
  • 31
  • 45
  • However if this option button is inside an Actionbar, it will be treated as a normal menu and you can check the other answer below for that case :) – Kushan Jul 08 '16 at 06:18
0

it's a common problem, Use Reflect make icon visible

@Override  
public boolean onCreateOptionsMenu(Menu menu)  
{  

    setIconEnable(menu, true);  

    return super.onCreateOptionsMenu(menu);  
}  


private void setIconEnable(Menu menu, boolean enable)  
{  
    try   
    {  
        Class<?> clazz = Class.forName("com.android.internal.view.menu.MenuBuilder");  
        Method m = clazz.getDeclaredMethod("setOptionalIconsVisible", boolean.class);  
        m.setAccessible(true);  
      m.invoke(menu, enable);  

    } catch (Exception e)   
    {  
        e.printStackTrace();  
    }  
}  
Cgx
  • 753
  • 3
  • 6