Tuesday 23 September 2014

Android Menu showAsAction Ignored

While working on a menu definition in Android you have the option to specify if the menu items should be visible as icons or should be hidden in the overflow menu (the three dots at the end of the action bar). This can be achieved with the showAsAction attribute in the menu XML definition. As a default the menu items are hidden in the overflow menu.

Today this attribute was simply ignored by my Android app. This is what my menu item definition looked like:

<item
        android:title="Search"
        android:id="@+id/menu_search"
        android:icon="@drawable/ic_action_content_copy"
        custom:showAsAction="always|collapseActionView"
        android:actionViewClass="android.support.v7.widget.SearchView"/>


But the item was not shown as an icon on the action bar. As you can see I'm using a custom namespace for the showAsAction attribute. The namespace is defined as:

xmlns:custom="http://schemas.android.com/apk/res-auto"

This is required if you are working with the compat library. What I didn't know, that was causing the issue, is that when you use the showAsAction with the custom namespace your Activity must extend the support ActionBarActivity, otherwise the attribute will just be ignored.

No comments:

Post a Comment