Android: Switching your app project from ActionBarSherlock to ActionBar in app compatibility library

The author of ActionBarSherlock Jake Wharton is no longer working on the library. Why? Because Google has released their version of his library and it works in (mostly) the same way.

There's no real reason to keep using ABS other than if you've abandoned your project or there is absolutely zero budget allocated to it. Even Wharton has recommended using the AppCompat library as it's ALMOST a complete drop-in replacement.

I'll run through a checklist of things you'll need to do in order to switch over your ActionBarSherlock based project to a AppCompat based project.

Switching over

First of all, close ActionBarSherlock in Eclipse. This will ensure your project will raise every error known to man, but it'll give you a starting point.

Now import an existing project from the SDK by right clicking on the Package Explorer > Import > General > Existing Projects into Workspace.

Now navigate to "AndroidSDK\extras\android\support\v7\appcompat" and select a project called "android-support-v7-appcompat".

Make sure "Copy projects into workspace" is UNTICKED. This makes it easier to upgrade the library via the SDK manager when updates are released.

Click Finish to import.

image image

Now right click on your app project > Properties > Android > Select ActionBarSherlock from the list of libraries and remove it. Add in "android-support-v7-appcompat".

While you still have the project properties open, add AppCompat to your build path by Java Build Path > Projects > Add > "android-support-v7-appcompat".

Android.xml

A few small changes are required here. Good thing is the minimum API level is still 7 for both ABS and AppCompat.

First up, the application theme. Change the following

<!-- before -->
<application android:theme="@style/Theme.Sherlock">

<!-- after -->
<application android:theme="@style/Theme.AppCompat">

This applies for all variances such as Theme.Sherlock.Light and Theme.Sherlock.Light.DarkActionBar, replace them with Theme.AppCompat.Light and Theme.AppCompat.Light.DarkActionBar respectively.

image
If in doubt, you can always autocomplete it.

Changing out SherlockActivity and SherlockFragmentActivity

We used to treat activities and fragment activities differently by inheriting them from different base classes. At least we can do away with that now by just using ActionBarActivity (from android.support.v7.app).

// Before
public class MainActivity extends SherlockFragmentActivity {

// After
public class MainActivity extends ActionBarActivity {

Rinse and repeat for all activities extending either SherlockActivity or SherlockFragmentActivity.

SherlockFragment to AppCompat Fragments

image

Similarly with SherlockFragment, simply search and replace them with the Fragment class. After saving, press Ctrl+Shift+O to fix up your imports.

image 
Be sure to use the Fragment from the support library.

// Before
import com.actionbarsherlock.app.SherlockFragment;
public class SurfaceViewFragment extends SherlockFragment {

// After
import android.support.v4.app.Fragment;
public class SurfaceViewFragment extends Fragment {

Again, if you're supporting API levels less than 11 then make sure you're importing from the compatibility library (android.support.v4.app) rather than API library.

Fixing up Menus

image

A quick search & replace can be done on getSupportMenuInflater() by replacing it with getMenuInflater().

If you're supporting any API levels below 11, a not-so-easy fix would be MenuItem.setShowAsAction(). In order to fix this, you'll need to use MenuItemCompat.setShowAsAction().

I don't have any magic solutions here, sorry.

// Before
menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

// After
MenuItemCompat.setShowAsAction(menu.getItem(i -1), MenuItemCompat.SHOW_AS_ACTION_ALWAYS);

I'm guessing it had to be done like that because the API level requirements were already set. Kinda sucks because it leads to slightly messier code though.

"Menu cannot be resolved to a type" is always a fun one. Simply open the file where that error is found and press Ctrl+Shift+O. If this doesn't work, make sure you've switched the activity over to ActionBarActivity.

That's all folks!

This may look like a long write-up, but I've added images and snippets to make it super clear. The process is pretty simple and repetitive so there's no real problems to run into.

The two libraries are mostly compatible and there's only minor changes required. The process of following the little red crosses took less than 5 minutes, but may take longer for projects which have many more activities/fragments.

9RRH5Tm
Almost as easy as translating Obama's speech into sign language.

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog