Show icons on overflow menu in contextual action bar

I know how to show icons in the ActionBar overflow menu. This is what I use

@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); } 

But when I click on any item in my ListView for a long time, CAB starts up. Now, when I open the CAB overflow menu, there are no icons in the menu. How can i do this?

Thanks in advance.

+5
source share
1 answer

I waited 2 days, but did not receive any answer. So he decided.

The idea here is quite simple. You need to create your own overflow element in and create a submenu to display the icon and text.

See the sample code below.

 <item android:id="@+id/overflow" android:icon="@drawable/ic_overflow_white" android:orderInCategory="201" android:title="@string/overflow" app:showAsAction="always"> <menu> <item android:id="@+id/cab_menu_select_all" android:icon="@drawable/ic_select_all_grey" android:orderInCategory="100" android:title="@string/cab_menu_select_all" app:showAsAction="always|withText"></item> </menu> </item> 

The trick here is to create submenus. You can add as many elements as you want.

+5
source

All Articles