Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To show icons in Overflow menu in ActionBar

I know it's not possible using the native API. Is there a workaround to implement that kind of view?

like image 629
Mihir Avatar asked Aug 22 '13 07:08

Mihir


People also ask

Where is the action overflow menu icon?

The right-hand side of the action bar shows the actions. The action buttons (3) show the most important actions of your app. Actions that do not fit in the action bar are moved to the action overflow, and an overflow icon appears on the right.

What is overflow menu icon?

4 Comments. The overflow icon is a common UI convention that's leveraged throughout Android to hide settings and other unimportant options. Google is now replacing it in the Play Store with a “tap & hold” gesture and bottom sheet menu.

How do I show the menu icon on Android?

Click res → New → Vector Asset . Choose the icon that you want by clicking on the android icon, click “Next” button and then click “Finish”. 6- Now we can add android menu items with icons, we will have 4 menu items. 1 menu item will be the root while the other 3 menu items will be grouped under a single Menu .


2 Answers

The previously posted answer is OK, generally speaking. But it basically removes the default behaviour of the Overflow menu. Things like how many icons can be displayed on different screen-sizes and then they dropped off into the overflow menu when they can't be displayed. By doing the above you remove a lot of important functionality.

A better method would be to tell the overflow menu to display the icons directly. You can do this by adding the following code to your Activity.

@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); } 
like image 94
Simon Avatar answered Sep 18 '22 12:09

Simon


In your menu xml, use the following syntax to nest menu, you will start getting the menu with icons

<item     android:id="@+id/empty"     android:icon="@drawable/ic_action_overflow"     android:orderInCategory="101"     android:showAsAction="always">     <menu>         <item             android:id="@+id/action_show_ir_list"             android:icon="@drawable/ic_menu_friendslist"             android:showAsAction="always|withText"             android:title="List"/>     </menu> </item> 

like image 35
iBabur Avatar answered Sep 16 '22 12:09

iBabur