icon in menu not showing in android

I want to add menu handler to my project. I read http://developer.android.com/guide/topics/ui/menus.html too, its very simple but the icon is not shown. I am very confused. I even added a menu item programmatically.

My code is:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0, 0, 0, "Quit").setIcon(R.drawable.ic_launcher);
    getMenuInflater().inflate(R.layout.menu, menu);
    return true;
}

and in xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Single menu item 
         Set id, icon and Title for each menu item 
    -->
    <item android:id="@+id/menu_bookmark" 
          android:icon="@drawable/update"
          android:title="@string/Update" />

</menu>

After Long try i found below solution which might help others to save there time. Basically, the solution provided by "lbarbosa", i like to thanks to him sincerely.

Tried this based on the previous answers and it works fine, at least with more recent versions of the support library (25.1):

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);

    if(menu instanceof MenuBuilder){
        MenuBuilder m = (MenuBuilder) menu;
        m.setOptionalIconsVisible(true);
    }

    return true;
}

If you're running your code on Android 3.0+, the icons in the menu are not shown by design. This is a design decision by Google.

You can read more about it in this on Android developers blog.


No matter what design choices where made by the system, you can circumvent this with the solution provided in the top upvoted answer to this question

Code below for completeness. Tested working on android.support.v7.app.ActionBarActivity

@Override
public boolean onMenuOpened(int featureId, Menu menu)
{
    if(featureId == Window.FEATURE_ACTION_BAR && menu != null){
        if(menu.getClass().getSimpleName().equals("MenuBuilder")){
            try{
                Method m = menu.getClass().getDeclaredMethod(
                    "setOptionalIconsVisible", Boolean.TYPE);
                m.setAccessible(true);
                m.invoke(menu, true);
            }
            catch(NoSuchMethodException e){
                Log.e(TAG, "onMenuOpened", e);
            }
            catch(Exception e){
                throw new RuntimeException(e);
            }
        }
    }
    return super.onMenuOpened(featureId, menu);
}

Old question but hope it will help someone.

use the following code:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item android:id="@+id/menu_item_share"
    android:title="Share"
    app:showAsAction="always"
    android:icon="@drawable/share" /></menu>

note i used app:showAsAction instead of android:showAsAction


You can add to your XML file the attribute android:showAsAction="always" inside your item element. It then will show the relevant menu option as an icon inside your action bar.

Note that it will be instead of the text in the menu.

For further read, look here under android:showAsAction.