pickle_inspector
pickle_inspector

Reputation: 1000

Android appcompat actionbar menu item showAsAction not working

I have a menu item that is showing up on android 4.x but not on 2.x. Here is my menu.xml

<?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/menu_filter"
    android:title="Filter"
    app:showAsAction="always"/>  
</menu>

This is my actionbar style

<style name="style1_actionBar" parent="@style/Widget.AppCompat.Light.ActionBar">
    <item name="android:background">@color/blue_dark</item>
    <item name="android:textColor">@color/white</item>
    <item name="actionMenuTextAppearance">@color/white</item>
    <item name="background">@color/blue_dark</item>
</style>

Any ideas?

Edit: removed double quote typo

Could it be the fact that I am showing only text, no icons? I'm kind of stuck here.

Upvotes: 26

Views: 29096

Answers (5)

Sandro Machado
Sandro Machado

Reputation: 10215

Using the menu in an activity that extends the AppCompact it is necessary import the app context in the XML and use it:

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

    <!-- "Mark Favorite", should appear as action button if possible -->
    <item
        android:id="@+id/action_favorite"
        android:icon="@drawable/ic_favorite_black_48dp"
        android:title="@string/action_favorite"
        app:showAsAction="ifRoom"/>

    <!-- Settings, should always be in the overflow -->
    <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          app:showAsAction="never"/>

</menu>

What you need to do basically is add xmlns:app="http://schemas.android.com/apk/res-auto"to the menu element in yout XML and use the showAsAction in the following format: app:showAsAction="ifRoom".

This will show the icon in the action bar, if possible.

Upvotes: 2

Mario Eraso
Mario Eraso

Reputation: 171

In my case I had to add a few lines to onCreateOptionsMenu.

Android Studio didn't let me use android:showAsAction="ifRoom" while using appCompat.

app:showAsAction="ifRoom" wasn't working and I removed it without problems.

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater  inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        for (int i = 0; i < menu.size(); i++) {
            menu.getItem(i).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        }
        return super.onCreateOptionsMenu(menu);
    }

Upvotes: 9

Ramesh
Ramesh

Reputation: 1381

<menu xmlns:android="http://schemas.android.com/apk/res/android"
  **xmlns:yourapp="http://schemas.android.com/apk/res-auto"** >

    <item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      **yourapp**:showAsAction="ifRoom"  />
</menu>

Please refer to the documentation. http://developer.android.com/guide/topics/ui/actionbar.html

Using XML attributes from the support library

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. So you must use your own namespace as a prefix for all attributes defined by the support library.

Upvotes: 20

pickle_inspector
pickle_inspector

Reputation: 1000

Whew, thanks for your help guys but I managed to figure it out. It wasn't a problem with the xml, it was a problem with the onCreateOptionsMenu function.

I was using this

new MenuInflater(getApplication()).inflate(R.menu.activity_wentry_editor, menu); 

instead of this

MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_wentry_editor, menu);

Not entirely sure why this works but it does.

Upvotes: 31

Android Learner
Android Learner

Reputation: 329

If you want your app to support action bar below 3.0 you need to use app compact v7 from the support library.

Also check the link

Upvotes: 0

Related Questions