Thursday, July 4, 2013

Using ActionBarSherlock in Android Studio (0.1.9)

Update 05 Aug 2013 : A better way to use Action Bar on Android 2.1 and above, see Using Action Bar on Android 2.1 (API Level 7) and above using Support Library (Rev. 18) in Android Studio 0.2.3

Preparing Android Studio Packages

Make sure the following packages are installed:

Preparing the Action Bar Sherlock Library

Download the Action Bar Sherlock library from or go to to get the latest version.

Creating the Android Project

Create a project in Android Studio called YourProject. Click Next to continue until you have created the project.

Your Project Root directory should look like this:

Importing Action Bar Sherlock Library to your Project

Open the Action Bar Sherlock Library you have downloaded earlier and extract it somewhere (for example to your Desktop). After extraction, the directory should look like this:

Copy the directory called "actionbarsherlock" to your Project Root directory. After copying, your Project Root directory should look like this:

Fixing your Gradles

First, modify the settings.gradle to tell Gradle that we have to include another project on our build. settings.gradle is located on your Project Root (YourApplicationProject/settings.gradle).

Let us include the actionbarsherlock directory in your settings.gradle:

Now that we have included the actionbarsherlock directory, we need to create a build.gradle file in the actionbarsherlock directory. We can copy the build.gradle file from our Application Directory (Note that there are two build.gradle file currently in our Project Directory. One is located at the Project Root Directory YourApplicationProject/build.gradle, another one is located at the Application Directory YourApplicationProject/YourApplication/build.gradle. The later is what we need to copy to YourApplicationProject/actionbarsherlock).

After copying, the actionbarsherlock directory should look like this:

Modify the copied build.gradle from the actionbarshelock directory. For the first change, plugin is changed from android to android-library. The second change will tell Gradle that this library is needed and include it once as compared to the original code where it will always include the library file even though it might have been included already on other build.gradle. This could cause issues when compiling the project. The last code block tells Gradle where are the relevant files and directory for it to compile the library.

Lastly, we need to modify the build.gradle from our Application Directory (YourApplicationProject/YourApplication/build.gradle) to include the actionbarsherlock library project and replace the compile file line with compile to prevent the errors when including libraries more than once.

Now your Gradle are ready to compile. But before you can start coding, we need to configure Android Studio and tell about the library.

Adding Action Bar Sherlock library to Android Studio

Go to File then Project Structure. In Project Settings, click the Modules from the list and then click the "+" plus sign on the next list as seen below:

Choose Import Module and Select the actionbarsherlock directory. Click OK when finished. On the Import Module dialog box, click Next until finished. Apply and then OK on the Project Structure Dialog Box. In Android Studio 0.1.9, I have to restart the IDE for the modules to appear in the List.

After restarting, Go to File then Project Structure again, and then select YourApplication from the list. On the right side, select Dependencies tab and click the "+" sign and choose Module Dependency.

Select the actionbarsherlock and then click Apply then OK to quit the Project Structure. Restart again the Android Studio.

You can now start coding your project using the Action Bar Sherlock.

Testing the Action Bar Sherlock Library

In, lets change the Activity to SherlockActivity. We need to remove the onCreateOptionsMenu override method as this does not exists.

Finally we need to modify the AndroidManifest.xml of our application to change the theme to the Action Bar Sherlock library themes.

Your application will now able to run the Action Bar Sherlock library for Android 2.X and above. This is an emulator running on Android 2.2:

I hope this helps you to get started with Action Bar Sherlock Library in Android Studio (0.1.9).

Updated on 2013/07/16 : Here is the source code for the whole project in a zip file.