Getting Started with ActionBarSherlock

Update: ActionBarSherlock is no longer necessary. The latest Google Support Library includes appcompat which is a better solution.

ActionBarSherlock is an Android support library designed to allow you to use the ActionBar which was introduced in Android 3.0 Honeycomb with older devices, back to Android 2.1 Eclair. This allows your applications to have a modern looking interface, even on older devices whose API does not support the new features.

To get started using ActionBarSherlock in Eclipse follow these steps.

Download ActionBarSherlock

Visit http://actionbarsherlock.com to download and extract the latest version to a development directory. You will be including this directory as a library in your app.

In Eclipse choose File -> New -> Other and select "Android Project from Existing Code". Select the "actionbarsherlock" folder withing the directory of the zip file you just extracted. Eclipse should detect the project and import it into your workspace.

Create a New Application

If you are going to add the ActionBar to an existing application skip this step.

Create a new Android application. I recommend the lowest minimum SDK you can use for your application (no lower that API 7). And set the target SDK to the highest version you can support (ideally the newest Android version). Finish the rest of the new application wizard so that Eclipse shows you your newly created application.

Adding ActionBarSherlock to the Application

Right click the application project and select properties. Then in the Android section under Library select Add and choose actionbarsherlock. Select Apply and then OK.

If you get the error: Found 2 versions of android-support-v4.jar in the dependency list in the console output then delete android-support-v4.jar in your project’s libs directory.

In AndroidManifest.xml add the attribute android:theme="@style/Theme.Sherlock" to the Application section. There is also Theme.Sherlock.Light,Theme.Sherlock.Light.DarkActionBar, Theme.Sherlock.Light.NoActionBar and Theme.Sherlock.NoActionBar.

Now you need to update each Activity that you want the ActionBar to appear on. In each Activity import com.actionbarsherlock.app.SherlockListActivity and remove the import for android.app.Activity. Change the class to extend SherlockActivity instead of Activity.

The ActionBar can also replace application menus. To use the ActionBar for menus import com.actionbarsherlock.view.Menu and com.actionbarsherlock.view.MenuItem. And remove the imports for android.view.Menuandroid.view.MenuInflater, and android.view.MenuItem. Inside onCreateOptionsMenu() replace getMenuInflater() with getSupportMenuInflater().

You will use an xml menu resource to define the ActionBar menu items just like you did for older Android menus; however items have some new options. Most importantly android:showAsAction. This defines whether the menu item is allowed to be shown on the dropdown menu if all the icons don’t fit on the actionbar. It also allows you to display just the item’s icon, or display its title. Below is a sample ActionBar menu I use on WIFI Key Recovery.

< ?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_backup"
        android:icon="@drawable/ic_action_save"
        android:title="@string/menu_backup"
        android:showAsAction="always|withText"></item>
 
    <item android:id="@+id/menu_refresh"
        android:title="@string/menu_refresh"
        android:icon="@drawable/ic_action_refresh"
        android:showAsAction="ifRoom"></item>
 
    <item android:id="@+id/menu_about"
        android:title="@string/menu_about"
        android:icon="@drawable/ic_action_about"
        android:showAsAction="ifRoom"></item>
 
    <item android:id="@+id/menu_settings"
        android:icon="@drawable/ic_action_settings"
        android:title="@string/menu_wifi_settings"
        android:showAsAction="ifRoom"></item>
 
</menu>

Bugs

Sometimes the android-support-v4.jar included in ActionBarSherlock may cause problems when using some of the newer API calls. I’ve noticed this when creating notifications with multiple actions. To fix this copy android-support-v13.jar from your Android SDK folder into ActionBarSherlock’s or your project’s libs folder. It will detect both versions of the support library and use the newest one. You should only need to do this last step if this is causing issues as ActionBarSherlock will likely be updated and fix this in a future release.