Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 332
pickle_inspector Avatar asked Feb 19 '14 05:02

pickle_inspector


3 Answers

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.

like image 51
pickle_inspector Avatar answered Nov 01 '22 11:11

pickle_inspector


<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.

like image 19
Ramesh Avatar answered Nov 01 '22 12:11

Ramesh


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);
    }
like image 9
Mario Eraso Avatar answered Nov 01 '22 13:11

Mario Eraso