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