Sunday, October 23, 2011

How to use ActionBar Sherlock

Note: ActionBarSherlock has changed significantly since the time of this posting. We will update this post as soon as we can, but in the mean time, please refer to the official documentation at http://actionbarsherlock.com/usage.html



  The Android ActionBar is a new widget that replaces the title bar, provides a location for menus and buttons, and provides a source of commonality between Android apps. The Action Bar is included by default in all activities that target Android 3.0 or greater, but in order to get the same effect in earlier versions of android, you were stuck writing a custom solution. ActionBar Sherlock is a nice library that allows you to solve this problem by letting you add ActionBars to your apps all the way back to Android 1.6. When used on Honeycomb and later it uses the default Android classes but on earlier versions it provides a custom replacement that still maintains compatibility with the newer API.

Here's a run down on how to add the library to your project. Note that since ActionBar Sherlock is an extension of the compatibility support library, you should remove the combatibility support jar if you were using it before since it is included with ActionBar Sherlock.

  1. Download the .zip/.tgz and extract it somewhere 
  2. Go to eclipse and choose File->New->Project 
  3. Choose Android Project 
  4. Select Create project from existing source and then browse to the library folder inside the folder you just extracted the .zip/.tgz into 
  5. Build Target should be the latest, but your minSdkVersion can be less 
  6. Finish the wizard, then right click on the newly created project and go to properties 
  7. Under the Android heading, you should see a section for Library with a checkbox IsLibrary. Make sure that's checked. 
  8. Now go to the properties for your Android project, then under the Android heading and the Library section choose Add... 
  9. You should see the actionbarsherlock library, add this to your project 
  10. Lastly, if you were using the compatibility support, you need to delete that jar since it's included in ActionBarSherlock After you have added the library to your project, you will need to take some additional steps to ensure it is being used.

1. Add a theme to your application in the AndroidManifest.xml

<application android:icon="@drawable/ic_launcher" android:label="@string/app_name"
 android:theme="@style/Theme.Sherlock">


2. Make sure that your activity extends FragmentActivity

public class AbbreviationsActivity extends FragmentActivity {


Now you're done! If you want to interact with your ActionBar through Java, make sure you use getSupportActionBar() instead of the standard getActionBar() to get the ActionBar Sherlock version of the action bar. The ActionBar Sherlock website along with the Android docs has information about theming, adding menus and buttons, and changing text and icons. Enjoy!

5 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. I belive you have to derive your activity from SherlockActivity and not FragmentActivity to use the sharlock actionbar.

    ReplyDelete
  3. Thank you so much for your step by step teaching

    ReplyDelete
  4. Congrats for the tutorial.
    I have an initial problem in Eclipse Juno there is no Select Create project from existing source option so, how can I add the library to my project??

    Thank you in advance

    ReplyDelete
    Replies
    1. Use Import -> Android -> Existing Android code into workspace

      Delete