Skip to content Skip to sidebar Skip to footer

How To Show Icons In Actionbar Overflow Menu?

I want to display icon in addition to menu item title in overflow dropdown menu. Is is possible?

Solution 1:

put your menu with property android:showAsAction="always" and in xml file of menu add sub menus under that your menu like this

<itemandroid:id="@+id/mainMenu"android:icon="@drawable/launcher"android:showAsAction="always"><menu><itemandroid:id="@+id/menu_logout"android:icon="@drawable/log_out"android:title="logout"/></menu></item>

this will show the icons in menus

Solution 2:

There is actually an easy way to achieve it, since what you see is actually a TextView, so you set a span , for example:

final MenuItem menuItem=...
final ImageSpan imageSpan=newImageSpan(this,R.drawable.ic_stat_app_icon);
final CharSequence title=" "+menuItem.getTitle();
final SpannableString spannableString=newSpannableString(title);
spannableString.setSpan(imageSpan,0,1,0);
menuItem.setTitle(spannableString);

This will put an icon at the beginning of the menu item, right before its original text.

Solution 3:

Hate answers like this but I am not good enough to give a better answer.

The icon for the overflow items for menu is the submenu2 line "subMenu2Item.setIcon(R.drawable.ic_launcher);" just replace ic_launcher with your icon file (in the res drawable directory of your program). Hope you mean using ActionBarSherlock.

The line subMenu1Item.setIcon(R.drawable.ic_launcher); is for the ActionBar icon for the Menu button at the top.

in response to "This is not possible by design"

Many thanks to Google for attempting to take over the world and letting us ride it's coat tails, BUT ... Google is too inconsistent to be grumpy over inconsistencies. It would be nice if Google could make an example for EVERY "set" of APIs they offer. There are API references to code, but no example about how to put them together, A whole completely working example that can be run as an APK is all I want. I can trick it out from there. If they can take the time to write the APK how much harder would it be to code a little example on a standard template? For 30% of everything it's not much to ask for. I had this same argument with Microsoft back in the day and it didn't happened then either. And thus and so you get stackoverflow. : ) Its a shame it takes some guy (rock on Jake Wharton) working on his own to do a better job with a backwards compatible Action bar than Google can come up with its ridiculous Action Bar Patch thingy.

@OverridepublicbooleanonCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {

    SubMenusubMenu1= menu.addSubMenu("Action Item");
    subMenu1.add("Help");
    subMenu1.add("About");

    MenuItemsubMenu1Item= subMenu1.getItem();
    subMenu1Item.setIcon(R.drawable.ic_launcher);
    subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    SubMenusubMenu2= menu.addSubMenu("All Options");
    subMenu2.add("Help");
    subMenu2.add("About");

    MenuItemsubMenu2Item= subMenu2.getItem();
    subMenu2Item.setIcon(R.drawable.ic_launcher);

    returnsuper.onCreateOptionsMenu(menu);
}

Solution 4:

@OverridepublicbooleanonMenuOpened(int featureId, Menu menu)
{
    if(featureId == Window.FEATURE_ACTION_BAR && menu != null){
        if(menu.getClass().getSimpleName().equals("MenuBuilder")){
            try{
                Methodm= menu.getClass().getDeclaredMethod(
                    "setOptionalIconsVisible", Boolean.TYPE);
                m.setAccessible(true);
                m.invoke(menu, true);
            }
            catch(NoSuchMethodException e){
                Log.e(TAG, "onMenuOpened", e);
            }
            catch(Exception e){
                thrownewRuntimeException(e);
            }
        }
    }
    returnsuper.onMenuOpened(featureId, menu);
}

Reference : How To show icons in Overflow menu in ActionBar

Solution 5:

Maybe you have the same problem as me. So for me solution was easy if you are using AppCompat just dont use this property:

android:showAsAction="always"

instead use it like this:

<?xml version="1.0" encoding="utf-8"?><menuxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"><itemandroid:id="@+id/option"android:title="@string/option"android:icon="@drawable/option"app:showAsAction="always"></item></menu>

There is difference in aditional xmlns:app and showAsAction is property of app.

Hope it helps someone.

Post a Comment for "How To Show Icons In Actionbar Overflow Menu?"