Using ActionBarSherlock in Android

Hello,

If you’re like me, you’re probably quite comfortable using Android APIs and making apps. But starting with Honeycomb (Android 3.0), there’s a new UI component called Fragment, and this changes everything. It’s quite annoying that Fragments are only available on 3.0 and upwards, and the market share of these platforms is only 8.2% as of May 1, 2012. Clearly, targetting only Honeycomb and ICS would be silly. Google has tried to work around that issue by releasing a compatibility library meant for applications using Android 1.6 and above, but a lot of the magic in ActionBar and Fragments is lost.

Enter ActionBarSherlock (ABS), an extension of the default compatibility library that works like a charm on Android 2.x versions. Setting up ActionBarSherlock, though, involved a bit of head-scratching for me. I use MOTODEV Studio, but these steps should work on Eclipse as well.

  1. Download the latest version (4.1.0 at the time of writing) of ActionBarSherlock and extract it
  2. Create a new Android Project and choose Create project from existing source, and browse to /library in the extracted folder. This will be used as a library project
  3. To make it a library project, open the Properties of the newly created project and under the Android section, check the “Is Library” option and click Apply.
  4. At this point, if you have a lot of errors (Fragment cannot be resolved to a type etc), open up Properties again, go into the Java Build Path section, and under the Libraries tab, click on Add External JARs, and browse to library/libs in the extracted folder and select android-support-v4.jar. This should get rid of the errors.
  5. Now that the library project is ready and error-free, it’s time to try a sample. Create a new Android Project, choose Create project from existing source once again, and select samples/demos in the extracted folder. This is a sample project that demonstrates how to use ABS.
  6. The library project needs to be associated with this project. To do so, open up the Properties of the Demos project, go to the Android section, and click Add and select the library project (usually com_actionbarsherlock). Click Apply.
  7. If this project also has a lot of errors, you may need to add android-support-v4.jar as an external JAR for this project. (Follow Step 4)
  8. Once you’re ready to make your own application, create a new Android project, making sure to set android:targetSDKVersion to the latest SDK (Android 15, 4.0.3 at the time of writing). Of course, com_actionbarsherlock will need to be associated with the new project as a library.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s