Show Menu item always in support action bar

Nitesh Kabra picture Nitesh Kabra · Mar 7, 2014 · Viewed 30.9k times · Source

enter image description hereI am working on android application . I have implemented supported action bar in it .I want to show option menu item always . But it is not showing . it is showing in drop down menu . my code for menu item given below.

<item
    android:id="@+id/action_settings"
    android:icon="@drawable/add_post"
    android:title="@string/action_settings"

    />

And code of ActionBar Activity is given below:-

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

any help will be appreciated.

Answer

fllo picture fllo · Mar 7, 2014

If you use the AppCompat ActionBar, you have to change your layout in this way:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto" >
    <item
        android:id="@+id/action_settings"
        android:icon="@drawable/add_post"
        android:title="@string/action_settings"
        app:showAsAction="always" />
</menu>  

showAsAction attribute will work only with a custom prefixe, see the documentation. According to it:

Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices.

Hope this helps.