Sticking with the theme of the official compatability library, ActionBarSherlock aims to allow the use of the action bar design pattern by mimicking the experience that would be found natively on Android Ice Cream Sandwich with as little effort as required.
First of all, wow. I did not expect anyone to recreate the Android ActionBar with such ease of use for older devices. They've stuck to their mission statement pretty well, kudos to the devs there!
This tutorial is a verbose re-write of the usage documentation for ActionBarSherlock, in case anybody ran into the same uncertainties I had when implementing it.
Requirements
- Java SDK 1.6+
- Must compile using Android 4.0+ (API level 14)
If you're missing any of these, you shouldn't continue with this guide.
Setup
- Edit "project.properties" and ensure that target is at least android-14 (Android 4.0 ICS)
- Open up ActionBarSherlock\library as a new Android project and compile it (Go to "Project" menu > Clean). Make sure build automatically is ticked.
- Leave the ActionBarSherlock project open!
- Add the ActionBarSherlock project to your app libraries.
Right click your project > Properties.
Click on Android > "Add" > And select ActionBarSherlock
- For the ActionBar to appear on your activity, extend said Activity from either SherlockActivity or SherlockFragmentActivity.
- Now it's time to set a default theme for your app.
You can use the layout editor to preview themes before setting one. From what I've seen, majority seem to choose between Theme.Sherlock or Theme.Sherlock.Light as they're most Google-esque.
Once you've decided on one, just use the manifest editor and browse for the right theme under the Application tab.
Or set the theme manually in your AndroidManifest.xml:
<application android:theme="@style/Theme.Sherlock">
Code compatibility
Most likely you'll get errors at any implementation of onCreateOptionsMenu().
Firstly, change any imports of Menu/MenuItem from "android.view" to "com.actionbarsherlock.view".
Now update the function a little.
// Old function, this will generate errors when you subclass SherlockActivity.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
this.getMenuInflater().inflate(R.menu.select, menu);
return true;
}
Replace with:
// New, Sherlock compatible function.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
this.getSupportMenuInflater().inflate(R.menu.select, menu);
return true;
}
Note the use of getSupportMenuInflater().
Rinse and repeat for all activities using SherlockActivity.
By now you should have something that is ready to test!
Enjoy your new toy!
A final note, if your app already has "android-support-v4.jar" included, feel free to remove it. The ActionBarSherlock library already has it included so including it twice may increase compilation time.