Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show icons in ActionBar overflow menu?

Tags:

I want to display icon in addition to menu item title in overflow dropdown menu.

Is is possible?

like image 787
Alexey Zakharov Avatar asked Aug 16 '12 07:08

Alexey Zakharov


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.

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 .

What is overflow menu icon?

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.


2 Answers

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

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

this will show the icons in menus

like image 78
androido Avatar answered Oct 11 '22 04:10

androido


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=new ImageSpan(this,R.drawable.ic_stat_app_icon); final CharSequence title=" "+menuItem.getTitle(); final SpannableString spannableString=new SpannableString(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.

like image 41
android developer Avatar answered Oct 11 '22 06:10

android developer