17

I'm following a book on Android Development to get myself started writing my first real app. I got up to the point where I'm making an options menu for one of my activities. The menu shows up, but the corresponding icon of the menu item refuses to display. Here is the code for the menu:

ReminderListActivity

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater mi = getMenuInflater();
        mi.inflate(R.menu.list_menu, menu);
        return true;
    }

res/menu/list_menu.xml

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

</menu>

I have copied the ic_menu_add.png icon (32x32px) from one of my Android SDK subfolders to my res/drawable-mdpi folder and renamed the file to menu_add.png. I refreshed the folder within eclipse so the icon shows up, and as you can see I set it as the icon for the menu item in my layout file. I tried running my project in the emulator a few times, but the icon never shows up. For the record, I am using Android 4.0.3..

Any ideas?

Jort
  • 1,319
  • 8
  • 21
  • 37
  • Can you try to put the icon in the density-less folder? I think it has nothing to do with your code. May be it is a glitch in Eclipse. – iTurki Jan 18 '12 at 11:14
  • What exactly do you mean by density-less folder? There are only ldpi, mdpi and hdpi drawables folders, no? I tried putting the icon in all folders, but that didn't fix it. – Jort Jan 18 '12 at 11:21
  • 1
    add a folder and name it 'drawable' then put the icon on it. If it didn't work, try to duplicate the icon on each folder. Don't forget to refresh the project and clean it before you test. – iTurki Jan 18 '12 at 11:25
  • Your code is fine as far as I am aware of !! – iTurki Jan 18 '12 at 11:27
  • I did all that, but still to no avail. Which is strange because when I change the icon in my list_menu.xml to an invalid name the project refuses to run. Therefore it must find my icon. Could it be that Android 4.0.3 doesn't display the icon if you have too few menu items? I only have one item for the moment, perhaps I should try adding some more with other icons and see what that does? I've also tested on both the emulator and my physical device (Nexus S), same result on both.. Edit: tried adding 3 menu items with different icons, still nothing.. Very peculiar.. – Jort Jan 18 '12 at 11:38
  • It is really odd !! The title is displayed, right? – iTurki Jan 18 '12 at 11:44
  • Yes. Only the title but not the icon. I just found a similar question on StackOverflow where someone only had the icon showing up and not the title, and that was apparently due to the icon being too big. Because my icon is taken from the standard drawables (32x32px) I figure the size should be good though. – Jort Jan 18 '12 at 11:48
  • Also added condensed titles for each menu item to make sure my titles weren't too long to obscure the icon, still nothing. I also scoured the properties to make sure there wasn't a property to set the display style or anything, but I really can't see anything that would influence this. – Jort Jan 18 '12 at 11:55
  • 1
    Is your icon the right color? I mean could it be blending with the background? Also, `drawables` is not a density less folder for things like icons, its used for shapes etc. If you have icons that are truly density independent (i.e solid color etc) they should go into the `drawable-nodpi` folder. – Code Poet Jan 18 '12 at 12:48
  • Yeah, I've also tried making it a different color, but to no avail. Adding a drawable-nodpi folder and putting my icons in there didn't help either. I also refreshed and cleaned before running. – Jort Jan 18 '12 at 12:55

3 Answers3

32

On Android 3.0+, the preferred approach for the options menu (a spillover menu in the action bar) will not show icons. If you have android:targetSdkVersion="11" or higher, icons will never show up in menus on Android 3.0+. The icons will show up if you promote an options menu item to be a toolbar button, and the icons will show up on Android 1.x/2.x devices.

CommonsWare
  • 910,778
  • 176
  • 2,215
  • 2,253
  • Thanks for the reply, I was suspecting it had something to do with Android's settings itself. Can I simply turn my options menu items into toolbar buttons or do I need to turn my entire options menu into a toolbar? – Jort Jan 18 '12 at 13:37
  • @Jort: Primarily, you should stop worrying about icons. You are welcome to use `android:showAsAction` to move options menu items into the action bar as toolbar buttons. – CommonsWare Jan 18 '12 at 14:00
  • Thanks for clarifying. The book I'm following atm is focused on writing apps for Android 2.2, that's why I was so persistant in trying to get my icons to show up. I haven't used the action bar yet, but now I understand how the UI changed in the newer versions of Android. Thanks again for the useful advice :) – Jort Jan 18 '12 at 14:35
8

This is perfectly working for me in API 23

<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:icon="@drawable/ic_menu"
    android:orderInCategory="100"
    android:title="Option Menu"
    app:showAsAction="always">
    <menu>
        <item
            android:id="@+id/action_myorder"
            android:icon="@drawable/ic_order"
            android:title="My Order" />
        <item
            android:id="@+id/action_myaccount"
            android:icon="@drawable/ic_account"
            android:title="My Account" />
        <item
            android:id="@+id/action_share"
            android:icon="@drawable/ic_share"
            android:title="Share" />
        <item
            android:id="@+id/action_term_condition"
            android:icon="@drawable/ic_terms"
            android:title="Term and Conditions" />
        <item
            android:id="@+id/action_logout"
            android:icon="@drawable/ic_logout"
            android:title="Logout" />
    </menu>
</item>

Ness Tyagi
  • 1,830
  • 21
  • 18
  • great! just want to know what is use of android:orderInCategory="100" – Abhijit Nov 30 '16 at 15:50
  • if the value for category is 0 then overflow menu come up on the fragment is grater then 0 it below the fragment. – Ness Tyagi Nov 30 '16 at 18:19
  • A helpful video that explains the steps: http://youtube.com/watch?v=GNGzmf_UcvY 'android how to add icon in options menu' – SJX Jul 16 '20 at 10:30
0

A good idea is that you created a layout with RelativeLayout and when user selected your menu, your layout is displayed.

Beachwalker
  • 6,930
  • 5
  • 47
  • 86
user3103823
  • 135
  • 1
  • 3
  • 13