Friday, September 27, 2013

Android: showAsAction when using ActionBar from support library v7

ActionBar as seen on Android Design Patterns1
Not so long ago Google finally updated their support library v7 to also include the ActionBar. As awesome as the 3rd-party ActionBarSherock library was, this finally allows us to get rid of it and only use "default" Android APIs (yes, I realize that the support libs are not exactly default, but whatever :P).
I'm not going to write about how to use the support library's ActionBar as Google provides great tutorials for that (maybe another time).
No. I just want to help out and when it comes to some small pitfalls – especially one using the ActionBar action items, formerly known as the menu.

The correct showAsAction

Usually when you develop apps you will be using the menu-resource files2 to declare a menu (nowadays called actions ;)). Inside the app you will inflate it as usual, but sometimes it's the case that the action items just won't show up.
Here's what happens: the android:showAsAction is ignored by the support library and instead you have to use your own custom namespace and not Android's default one!
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:awesomeapp="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/actionAwesomeButton"
        android:title="@string/awesome"
        awesomeapp:showAsAction="ifRoom"
        android:icon="@android:drawable/ic_action_awesome" />
</menu>
This is quite simple but will do the trick. Of course you can use any name for the namespace as well as all the possible showAsAction values – just don't forget about it.

Portions of this page are reproduced from work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
2 If you're not yet using menu resource files: start now, no matter how small and insignificant your project is! ;)

Labels: , , , , , , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home