Action Bar not showing action view icons

Jumpa picture Jumpa · Aug 7, 2013 · Viewed 35.3k times · Source

I'm using the new Action Bar Support and all my action views are shown in overflow and not as icons in the bar. My app is for 7+ API.

HomeActivity:

public class HomeActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle(R.string.app_name);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);
    }

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

home.xml

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>
<item
    android:id="@+id/action_browse"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/title_activity_browse"/>
<item
    android:id="@+id/action_search"
    android:actionViewClass="android.widget.SearchView"
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="ifRoom|collapseActionView"
    android:title="@string/text_search"/>
<item
    android:id="@+id/action_scan"
    android:icon="@drawable/action_scan"
    android:showAsAction="always"
    android:title="@string/title_activity_browse"/>

I'm deploying on Nexus 7 with 4.3 and on LGP500 with 2.3.3 and no icons. I've also added android:theme="@style/Theme.AppCompat.Light.DarkActionBar" on the manifest and my project is correctly referencing android-support-v7-appcompat as described in the official doc.

Answer

Mario Lenci picture Mario Lenci · Aug 7, 2013

you have to define your menu resource files with also the attributes for the support library.

To implement the back support it reads them instead of the ones defined in older Android version.

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

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    yourapp:showAsAction="never"
    android:title="@string/action_settings"/>
<item
    android:id="@+id/action_browse"
    android:orderInCategory="100"
    android:showAsAction="never"
    yourapp:showAsAction="never"
    android:title="@string/title_activity_browse"/>
<item
    android:id="@+id/action_search"
    android:actionViewClass="android.support.v7.widget.SearchView"
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="ifRoom|collapseActionView"
    yourapp:showAsAction="ifRoom|collapseActionView"
    yourapp:actionViewClass="android.support.v7.widget.SearchView"
    android:title="@string/text_search"/>
<item
    android:id="@+id/action_scan"
    android:icon="@drawable/action_scan"
    android:showAsAction="always"
    yourapp:showAsAction="always"
    android:title="@string/title_activity_browse"/>

</menu>

NB remeber that for the SearchView class changed. it's now used the one from the support library so you also have to update your code in the onCreateOptionsMenu()

EDIT: here is a pretty good tutorial on how to migrate from ActionBarSherlok to AppCompat