Reputation: 11948
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>
Upvotes: 47
Views: 95366
Reputation: 149
I put explicit icons in onCreateOptionsMenu method using below code
for (int i = 0; i < menu.size(); i++) { MenuItem item = menu.getItem(i);
if (item.getItemId() == R.id.print) {
item.setIcon(getDrawable(R.drawable.print));
} }
Upvotes: 1
Reputation: 3658
Just add this attr inside item tag in your menu.xml
app:showAsAction="always
Upvotes: -1
Reputation: 101
Working with android.support.v7.app.AppCompatActivity
, is making the input for the icons a very difficult task. You need to implement onMenuOpen
, super method from AppComactActivity
class. After this check, if the menu is not null. If the menu is not null pass Method class like this and setOptionalIconsVisible
to true with boolean.
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
if(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("MAIN", "onMenuOpened", e);
}
catch(Exception e){
throw new RuntimeException(e);
}
}
}
return super.onMenuOpened(featureId, menu);
}
Upvotes: 0
Reputation: 927
/* menu code */
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/one" android:title="one"
android:icon="@mipmap/ic_launcher" app:showAsAction="always" />
<item android:id="@+id/two" android:title="two"
android:icon="@mipmap/ic_launcher" app:showAsAction="always" />
<item android:id="@+id/three" android:title="three"
android:icon="@mipmap/ic_launcher" />
</menu>
/* Java code */
@SuppressLint("RestrictedApi")
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mymenu,menu);
if(menu instanceof MenuBuilder){
MenuBuilder m = (MenuBuilder) menu;
m.setOptionalIconsVisible(true);
}
return true;
}
Upvotes: 4
Reputation: 661
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;
}
Upvotes: 49
Reputation: 401
Old question but hope it will help someone.
use the following code:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/menu_item_share"
android:title="Share"
app:showAsAction="always"
android:icon="@drawable/share" /></menu>
note i used app:showAsAction
instead of android:showAsAction
Upvotes: 19
Reputation: 27
Forget all those, do this step.
add app:showsAsAction="always"
to your item. If you use android:showsAsAction="always"
you won't get the solution.
Try with adding app attribute to your item.
Upvotes: 1
Reputation: 1517
No matter what design choices where made by the system, you can circumvent this with the solution provided in the top upvoted answer to this question
Code below for completeness. Tested working on android.support.v7.app.ActionBarActivity
@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);
}
Upvotes: 28
Reputation: 5121
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, 0, 0, "androidDemo").setIcon(R.drawable.ic_launcher);
getMenuInflater().inflate(R.layout.menu, menu);
return true;
}
and in xml:
<item android:id="@+id/menuUpdate"
android:icon="@drawable/update_icon"
android:title="@string/Update"
android:showAsAction="always"/>
Upvotes: -2
Reputation: 3155
You can add to your XML file the attribute android:showAsAction="always"
inside your item element. It then will show the relevant menu option as an icon inside your action bar.
Note that it will be instead of the text in the menu.
For further read, look here under android:showAsAction.
Upvotes: 6