19

How to show icon with option menu.I have tried the following code but my option menu is without image icon.I am using android version 4.0 for developing app.

Java code :

 public boolean onCreateOptionsMenu(Menu menu) {
            super.onCreateOptionsMenu(menu);
             menu.add("Add Contacts").setIcon(
                    R.drawable.ic_launcher);

            return true;
        }

Following is my app's screen shot

enter image description here

I need image to be displayed on the top of "Add Contacts" item.

sonia
  • 405
  • 2
  • 7
  • 23

9 Answers9

4

Override OnPrepareOptionsMenu and add icon from there too

and if its for above 3.0, use android:showAsAction in xml.

eg. android:showAsAction="ifRoom|withText"

xmen
  • 1,947
  • 2
  • 25
  • 47
4

I tried the code in two line and it works:

public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add("Add Contacts");
        menu.getItem(0).setIcon(R.drawable.ic_launcher);
        return true;
}
Panthesilea
  • 123
  • 9
3

You can create a custom menu like this:

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

And then inflate it

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

More on this here: http://developer.android.com/guide/topics/ui/menus.html#options-menu

nedaRM
  • 1,837
  • 1
  • 14
  • 28
  • http://stackoverflow.com/questions/8306943/android-menu-icons-are-not-displaying-when-the-api-level-is-above-10 – nedaRM Feb 21 '13 at 06:37
2

you can directly set this into the xml file.

  <item android:id="@+id/add_contacts"
  android:icon="@android:drawable/plus_icon"
  android:title="Add Contacts"/>
akash yadav
  • 349
  • 4
  • 14
  • This works for me if you're showing it as an action, but if you're not (ie a dropdown options menu), it doesnt display - at least for me anyway – trainmania100 Jan 10 '22 at 14:57
2

You Can try Following this Link.

Check this out and tell me if it worked or not.

Or you can do some thing like this.
Create menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:id="@+id/next"
              android:icon="@drawable/ic_next"
              android:title="@string/next" />
      <item android:id="@+id/previous"
            android:icon="@drawable/ic_previous"
            android:title="@string/previous" />
      <item android:id="@+id/list"
            android:icon="@drawable/ic_list"
            android:title="@string/list" /> 
</menu>

And now you will be able to set ICON on menu

Now in CreateOptionMenu

public boolean onCreateOptionsMenu(Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.menu, menu);
      return true;
    }

And to access that menu.

public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.next:
            Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.next) + " menu option",
                        Toast.LENGTH_SHORT).show();
            return true;
      …
      default:
            return super.onOptionsItemSelected(item);
      }
   }
Si8
  • 9,141
  • 22
  • 109
  • 221
Rahul Gokani
  • 1,688
  • 5
  • 25
  • 43
2

To enable Option Menu with Icons:

Wrap the Items with an Item with showAsAction="always" and a Menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:title="@string/title_menu"
        android:icon="@mipmap/ic_icon_menu"
        app:showAsAction="always">
        <menu>
            <item
                android:id="@+id/action1"
                android:orderInCategory="100"
                android:title="@string/action1"
                android:icon="@mipmap/ic_icon_action1"
                app:showAsAction="never" />
        </menu>
    </item>
</menu>
Benny
  • 2,233
  • 1
  • 22
  • 27
1

If you use some following attribute in manifest file then it's will be show your icon....

<activity android:name=".ui.CategoryActivity"
        android:label="@string/app_name"
        **android:theme="@android:style/Theme.NoTitleBar"**></activity>

It's work fine for me...:) +1 for my own effort...

**must be enter.

Sham
  • 72
  • 7
1

Easiest way is to use the @drawable only when setting your menu item.

OR

Simply put the @drawable before the title declaration.

<?xml version="1.0" encoding="UTF-8" ?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app    ="http://schemas.android.com/apk/res-auto">
<item
  android:id      ="@+id/addToFavorites"
  android:icon = "@drawable/ic_favorite_border_white_24dp"
  android:title = "Hello"
  app:showAsAction="always" />
<item
  android:id      ="@+id/about"
  android:title   ="About"
  app:showAsAction="never" />
</menu>  
VSB
  • 317
  • 2
  • 10
-1

the problem is the Androidmanifest.xml. Remove android:theme="@style/AppTheme" and it will work just fine

Dany NA
  • 65
  • 1
  • 9