1

I am having trouble getting the menu icon to show up on the action bar. I tried setting

android:showAsAction="always"

but even that doesn't work. In an earlier project I just used

android:showAsAction="ifRoom|withText"

and that worked. The only thing different with this project is that I am building with gradle instead of ANT. Could that make a difference?

Here is my menu xml:

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item
    android:id="@+id/new_transaction"
    android:icon="@drawable/ic_menu_add"
    android:title="@string/new_transaction"
    android:titleCondensed="@string/new_transaction_condensed"
    android:showAsAction="always" />

</menu>

I copied each version of ic_menu_add.png from the android library to each respective drawable folder.

And here is my onCreateOptionsMenu in my ActionBarActivity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.account_pager, menu);
    return true;
}

No matter what I try, the icon does not show up. I've tried changing the max and min API level, but to no avail. In order to click the menu item, I have to go to the overflow menu and then click the dropdown menu item there, which is definitely less than ideal. How can I make the icon show?

My min API level is 11, and my max is 20. I am developing on an HTC One running Android 4.1.2, if any of that matters. Thanks in advance.

NMandapaty
  • 31
  • 4
  • Same question? [stackoverflow.com/questi...](http://stackoverflow.com/questions/19750635/icon-in-menu-not-showing-in-android) – aemre Oct 11 '14 at 19:08

1 Answers1

2

Try this:

    @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("", "onMenuOpened", e);
        } catch (Exception e) {
          throw new RuntimeException(e);
        }
      }
    }
    return super.onMenuOpened(featureId, menu);
  }
D4rjeeling
  • 21
  • 2