Search code examples
androiddrop-down-menumenuandroid-actionbar

Android menu item set to always be shown is not being shown


Using app:showAsAction="always" for a menu item in Android is not showing the menu item in the Action Bar. Instead, the item is shown in a drop-down together with menu items having app:showAsAction="never". Here are the codes:

menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="myAppID.MainActivity" >
<item android:id="@+id/action_one"
    android:title="One"
    app:showAsAction="always" />
<item android:id="@+id/action_two"
    android:title="Two"
    app:showAsAction="never" />
<item android:id="@+id/action_three"
    android:title="Three"
    app:showAsAction="never" />
</menu>

MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

Additional info: I am not using AppCompat. I noticed that when AppCompat is used, the behaviour of the menu item is correct.

How can I fix this without AppCompat?


Solution

  • Instead of app:showAsAction change it to android:showAsAction as shown:

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          tools:context="myAppID.MainActivity" >
          <item android:id="@+id/action_one"
              android:title="One"
              android:icon="@drawable/your_icon"
              android:showAsAction="always" />
          <item android:id="@+id/action_two"
              android:title="Two"
              android:showAsAction="never" />
          <item android:id="@+id/action_three"
              android:title="Three"
              android:showAsAction="never" />
      </menu>
    

    Hope it works.