I want to add menu handler to my project. I read http://developer.android.com/guide/topics/ui/menus.html too, its very simple but the icon is not shown. I am very confused. I even added a menu item programmatically.
My code is:
@Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, 0, 0, "Quit").setIcon(R.drawable.ic_launcher); getMenuInflater().inflate(R.layout.menu, menu); return true; }
and in xml:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Single menu item Set id, icon and Title for each menu item --> <item android:id="@+id/menu_bookmark" android:icon="@drawable/update" android:title="@string/Update" /> </menu>
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 .
After Long try i found below solution which might help others to save there time. Basically, the solution provided by "lbarbosa", i like to thanks to him sincerely.
Tried this based on the previous answers and it works fine, at least with more recent versions of the support library (25.1):
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); if(menu instanceof MenuBuilder){ MenuBuilder m = (MenuBuilder) menu; m.setOptionalIconsVisible(true); } return true; }
If you're running your code on Android 3.0+, the icons in the menu are not shown by design. This is a design decision by Google.
You can read more about it in this on Android developers blog.
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