I want to display icon in addition to menu item title in overflow dropdown menu.
Is is possible?
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.
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 .
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With