Reputation: 3399
Whilst I can set Action Bar icons in my Java code, I'm unable to get them to display when I set them in my XML. I'd prefer set them in XML.
Below are my two attempts to achieve the same thing - three icons on the Action Bar. Could you explain why the second approach fails?
(Note, In my final version, I'm intending to keep two of the three in the overflow menu, I'm just trying to show all three during testing.)
This XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menuConfig"
android:title="Configuration"
/>
<item android:id="@+id/menuAbout"
android:title="About"
/>
<item android:id="@+id/menuHelp"
android:title="Help"
/>
</menu>
And this Java:
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.optionsmenu, menu);
MenuItem settings = menu.findItem(R.id.menuConfig);
settings.setIcon(R.drawable.settings);
settings.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
MenuItem about = menu.findItem(R.id.menuAbout);
about.setIcon(R.drawable.about);
about.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
MenuItem help = menu.findItem(R.id.menuHelp);
help.setIcon(R.drawable.help);
help.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
Produce this display:
Whereas, this XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menuConfig"
android:title="Configuration"
android:icon="@drawable/settings"
showAsAction="ifRoom"
/>
<item android:id="@+id/menuAbout"
android:title="About"
android:icon="@drawable/about"
showAsAction="ifRoom"
/>
<item android:id="@+id/menuHelp"
android:title="Help"
android:icon="@drawable/help"
showAsAction="ifRoom"
/>
</menu>
And this Java:
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.optionsmenu, menu);
return true;
}
Produce this display:
Upvotes: 1
Views: 2674
Reputation: 3025
That's ok.
The overflow menu only shows text. The icons are only used when the Item is shown in the action bar as an action.
If you want to see the icons in the action bar you have to use
android:showAsAction="ifRoom"
instead of
showAsAction="ifRoom"
Upvotes: 2