Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionBarSherlock - Show overflowed action items as icon + text

Context

I am creating an action bar with some action items, using ActionbarSherlock. Some of them are overflowed, so they are shown in the overflow submenu.

My problem

Those overflowed items are shown in the submenu just as mere texts.

I have tried MenuItem.SHOW_AS_ACTION_IF_ROOM and MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT for setShowAsAction() but both have the same behavior:

Overflowed items without icon

What I want

Is it possible to show these items in the overflow submenu as <their_icon> + <their_text>? Something like this:

Overflowed items with icon

like image 945
Christian García Avatar asked May 16 '12 09:05

Christian García


3 Answers

I don't understand either why submenus can have icons and menus only text. Maybe adding menu items as submenus to the root menu could help you. :)

Just a mention, adding icons like this will force you to use the ActionBar (Sherlock) and not the standard way of displaying menus at the bottom when pressing the menu key in older versions of Android.

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/root_menu"
    android:icon="@drawable/ic_menu_moreoverflow_normal_holo_light"
    android:showAsAction="always"
    android:title="More">
    <menu>
        <item
            android:id="@+id/menu_settings"
            android:icon="@drawable/ic_menu_settings_holo_light"
            android:showAsAction="never"
            android:title="Settings" />
        <item
            android:id="@+id/menu_about"
            android:icon="@drawable/ic_menu_info_details"
            android:showAsAction="never"
            android:title="About"/>
   </menu>
</item>
</menu>
like image 64
Arise Avatar answered Nov 19 '22 07:11

Arise


1, change the value to true of "mOptionalIconsVisible" in com.actionbarsherlock.internal.view.menu.MenuBuilder.java. Then you will see the icons always. Only work for ActionBarSherlockCompact.java.

2, Change source code to add an api for MenuBuilder.java and MenuWrapper.java to set the icon.

like image 35
rohn.chan Avatar answered Nov 19 '22 09:11

rohn.chan


I have a work around for such scenario. If you are sure about what you want to put in overflow menu, then you can put them in a submenu, and you can put the icon and title for that sub menu item along with sub menus by overriding onCreateOptionsMenu method.

This is what I have done to achieve this functionality:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    menu.add(0, 1, 1, "MENU ONE TITLE")
    .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    SubMenu subMenu = menu.addSubMenu(0, 0, 2, "MENU TWO TITLE");

    subMenu.add(0, 2, 2, "SUB MENU ONE TITLE")
    .setIcon(R.drawable.sub_menu_one_icon);

    subMenu.add(0, 3, 3, "SUB MENU TWO TITLE")
    .setIcon(R.drawable.sub_menu_two_icon);

    MenuItem subMenuItem = subMenu.getItem();
    subMenuItem.setIcon(R.drawable.menu_two_icon);
    subMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    return super.onCreateOptionsMenu(menu);
}
like image 5
Atul Kaushik Avatar answered Nov 19 '22 07:11

Atul Kaushik