sonia
sonia

Reputation: 405

android option menu with icon

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.

Upvotes: 19

Views: 80804

Answers (9)

Benny
Benny

Reputation: 2331

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>

Upvotes: 2

VSB
VSB

Reputation: 327

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>  

Upvotes: 1

Rahul Gokani
Rahul Gokani

Reputation: 1708

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);
      }
   }

Upvotes: 2

Panthesilea
Panthesilea

Reputation: 123

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;
}

Upvotes: 4

Dany NA
Dany NA

Reputation: 65

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

Upvotes: -1

xmen
xmen

Reputation: 1967

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"

Upvotes: 4

Sham
Sham

Reputation: 72

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.

Upvotes: 1

akash yadav
akash yadav

Reputation: 349

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"/>

Upvotes: 2

nedaRM
nedaRM

Reputation: 1827

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

Upvotes: 3

Related Questions