· 2 min read
ActionBarCompat (Part 3): Migrating from ActionBarSherlock
After learning some basic rules to work with ActionBarCompat, you will probably need to migrate some apps from ActionBarSherlock to ActionBarCompat, because it provides better integration with some other functionalities such as Navigation Drawer, and there are some issues that you must know to make it easy. I’ll be replacing Bandhook Action Bar while writing this post, so there may be some points I won’t cover if I’m not using them. Please feel free to explain in comments section and I will add it.
< ActionBarCompat (Part 2): Action Views
Import AppCompat project
As I explained on a previous post, an apklib project is required to use ActionBarCompat. Gradle allows some easier integrations in Android Studio, but I can imagine most of us still use Eclipse or IntelliJ, so the easiest way will be to import as a project or module. Remember you can find it at sdk\extras\android\support\v7\appcompat. Adding both jars under libs into the main project may also be necessary.
Replace SherlockFragmentActivity with ActionBarActivity
ActionBarActivity is the new activity to be used with ActionBarCompat, and it allows the use of fragments. If you have any other kind of Sherlock Activities, replace them too. There shouldn’t be any problems even if you are not using fragments.
Replace SherlockFragment with Fragment
Use support-v4 fragments instead of the ones in ActionBarSherlock.
Change Menu, MenuItem and getSupportMenuInflater() references
AppCompat will use native ones instead. ActionBar package reference must also be modified. Modify menu xml resources as explained in previous post.
Modify the way you get Action Views
If you read part 2 of this series, I explained there’s a compatibility class to do this task. It won’t fail on new versions if you don’t change it, but old versions will crash.
mSearchView = (SearchView) MenuItemCompat.getActionView(mSearchItem);
Modify your Themes and Styles
Make your theme extend some of the AppCompat ones. For instance, you can use: Theme.AppCompat.Light.DarkActionBar. Replace every Sherlock appearance in styles with AppCompat. Alternatively you can use Action Bar Style Generator, which is already optimized to work with AppCompat.
And that’s all! I got ActionBarCompat working on Bandhook easier than I expected. Jake Wharton did a great job, as he explained in its own blog, when creating its library to be easily replaced.