Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android options menu icon won't display

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?

like image 689
Jort Avatar asked Jan 18 '12 11:01

Jort


People also ask

Where is Options menu in Android?

Android Option Menus are the primary menus of android. They can be used for settings, search, delete item etc. When and how this item should appear as an action item in the app bar is decided by the Show Action attribute.

How do I show the menu bar on Android?

This is going to be in res/menu/main_menu . Right click the res folder and choose New > Android Resource File. Type main_menu for the File name. Choose Menu for the Resource type.


2 Answers

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.

like image 110
CommonsWare Avatar answered Sep 27 '22 17:09

CommonsWare


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>

like image 24
Ness Tyagi Avatar answered Sep 27 '22 19:09

Ness Tyagi