Using an ActionBar in your Android app with ActionBarSherlock

First of all, who am I?

Hey there! I’m Xavi Rigau, a 21 year old Spanish student (I’ll finish my IT degree this summer) who loves Android Development. I started programming for Android as a hobby in the summer of 2009, but when I started university again in October I left it for a while.

Then I started my internship in a company called Tempos 21 in October, 1st 2010. It’s a consultant specialized in mobility. First day I did some boring stuff because I was supposed to be in the Testing department. The problem was that there was only one Android developer (which name is Paulo) at that time and when I said that I had programmed a little on Android they thought it should be better if I could help Paulo a little. Since then I’ve been working there as an Android developer, so I’ve been into this 1 year and a half now.

The ActionBar pattern

In my first post I’ll explain how to put an ActionBar in your Android app in a really simple way. For those who are new to Android Development, an ActionBar is a design pattern that shoud be used in almost all applications. You can find some info about Action Bar design at the Android Design site and the documentation of the ActionBar class at the Android Developers site, but summing up, what the Android team says is that the ActionBar should be used replacing the old Options Menu.

The problem

All of this is really cool and marvelous, but the fact is that there’s no real implementation of an Action Bar since Android 3.0 (Honeycomb), so if your application targets Android 3.0 or higher, when you run it on versions >= 3.0 you’ll get the default ActionBar, but if you run the same application on versions < 3.0 you won’t. Okay, you can download and use the Compatibility Library in order to use ActionBar in devices with Honeycomb or later and the standard options menu in other cases, but that’s not a real solution.

The solution

So, how do we get the ActionBar on pre-Honeycomb? It’s really easy thanks to Jake Wharton, a guy who whas created a library called ActionBarSherlock. What this library does is that if you’re using your app in a device with Android 2.x, it gives you an ActionBar that is a copy of the real ActionBar (it behaves the same way), but if you’re on Android 3.0 or later, you’ll get the standard ActionBar. The cool thing is that the methods that you can use with this ActionBar are the same than with the standard one.

The code

I’ll suppose that you have a proper environment configured to develop on Android. If not, take a look at the steps or look at this tutorial.
I’ll also assume that you know how to create an Android project and things like that. In other cases you should read this tutorial or this one.

Okay, let’s do it! Here are the steps to get the library into Eclipse:

  1. First of all, go to Sherlock ActionBar library’s website and Download the latest version (current is 4.0.0).
  2. Then open the file you downloaded, enter the root folder and you will see three folders: library, samples and website.
  3. Extract the library folder into your workspace folder. You can rename it if you want to something like “Sherlock-ActionBar-lib-4.0.0”.
  4. Open Eclipse. Click File -> New -> Android Project.
  5. Now select “Create project from existing source”, and select the folder that you copied into your workspace folder. You should also type a name such as “Sherlock-ActionBar-lib-4.0.0” :). Now press Next.
  6. Select Android 4.0 as target SDK and press Finish.
  7. Open the libs folder in the Project that we’ve created, right-click the “android-support-v4.jar” file and select Build Path -> Add to Build Path.

Now, we have the Android Library Project of the Sherlock ActionBar in our workspace and in Eclipse.

Next step is to create a new Android Project, targeting at least Android 4.0 SDK. We will call ActionBarTest.

Then right-click on that ActionBarTest project and select Properties -> Android and add the “Sherlock-ActionBar-lib-4.0.0” as a Library.

Now, open open the default Activity that Eclipse has created, it name should be ActionBarTestActivity.java and change the following line:

public class ActionBarTestActivity extends Activity {

to:

public class ActionBarTestActivity extends SherlockActivity {

Finally here it comes the last step: Open the AndroidManifest of your ActionBarTest project and write inside the application tag:

android:theme=&quot;@style/Theme.Sherlock.Light.DarkActionBar&quot;

So, you will have something like:

&lt;application
	android:icon=&quot;@drawable/ic_launcher&quot;
	android:label=&quot;@string/app_name&quot;
	android:theme=&quot;@style/Theme.Sherlock.Light.DarkActionBar&quot; &gt;

	&lt;activity
		android:name=&quot;.ActionBarTestActivity&quot;
		android:label=&quot;@string/app_name&quot; &gt;
		&lt;intent-filter&gt;
	&lt;!-- ... Other stuff... --&gt;

And that was all guys! That’s the result:

UPDATE: You can find here a videotutorial about ActionBarSherlock which I found really interesting!

Tags: , , ,

About Xavi Rigau

I'm an Android enthusiast! I'm finishing a degree in IT science while I work as Android Developer in a company in Barcelona. I also try to develop my own apps. My dog Fiona helps me a lot in that! She's quite good.

16 responses to “Using an ActionBar in your Android app with ActionBarSherlock”

  1. ewoks says :

    Que bueno!!!

    Just wanted to welcome u in Android wonderland! 🙂

  2. Waqas says :

    Hola!
    welcome & keep up the good work

  3. Alva says :

    Great post! Thank’s a lot 🙂

  4. sagar says :

    getting a jar mismatch error…
    saying fix your dependencies…

  5. Dennis says :

    Simple and Great. Thanks Buddy 🙂

  6. Sagar Trehan says :

    when I import library in my eclipse from workspace it gives me a lot of errors.I have already added the support library.please tell me what i do now

  7. ALB says :

    Thank you for this very helpful tutorial!

  8. igor ganapolsky says :

    Why do you say:
    “you can download and use the Compatibility Library in order to use ActionBar in devices with Honeycomb or later and the standard options menu in other cases, but that’s not a real solution.”?

    It sure is a real solution. What is wrong with Google’s compat library?

  9. Dipasri says :

    Thank you. you helped a lot.

  10. Apocalypse Preppers says :

    Good information. Lucky me I ran across your blog by chance (stumbleupon).
    I’ve book marked it for later!

Leave a comment