Support v7 appcompat & options menu

Some time ago, Google made its own backport of the famous ActionBar. The times of ActionBar Sherlock are finally over, and that’s a good thing. There has already been written a lot about using the Support ActionBar, so I won’t bother you with that. I’m going to talk about a small detail, but yet a very important one: using the Options Menu.

ActionBar Share

People love to share their favourite content with friends and family. Adding these action buttons to the ActionBar is pretty easy, just define a menu in XML and inflate it in your Activity or Fragment.

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:bcmc="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_share"
        android:title="@string/label_share"
        android:icon="@drawable/ic_action_share"
        android:showAsAction="always"
        bcmc:showAsAction="always"/>

</menu>

Notice that the showAsAction attribute above uses a custom namespace defined in the <menu> 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.

If you forget to duplicate the showAsAction attribute, you will be looking for hours why your action buttons don’t appear in the ActionBar, and then come to the discovery that you wasted your time on such a small detail.