What are the common issues when migrating from ActionBarSherlock to ActionBarCompat?

I would like to remove ActionBarSherlock from my application and replace it with the standard ActionBarCompat.

  • How do I implement ActionBarCompat?
  • How do I migrate the Activites?
  • Which imports replace the ActionBarSherlock imports?
  • What are typical problems?

  • Tutorials for Flurry Analytics in Android ?
  • Java method for android:layout_gravity
  • runOnUiThread inside a View
  • Updating Android SDK: “A folder failed to be moved.”
  • google map api v2, and older version of google play services lib
  • How To Show and hide ActionBar with AppCompat v.7
  • Related posts:

    Picasso loads into wrong imageview when using view holder pattern
    I need user's email address after successful facebook login in android using SDK 4.0
    sqlite database not created
    Android : Required API to use Switch?
    ListFragment onPrepareOptionsMenu called before onCreate. Why and How to Fix / Bypass?
    SD HTTP RESPONSE CODE:200 error with Flurry
  • com.android.ddmlib.InstallException: Failed to establish session react-native
  • Android Studio doesn't launch: Could not find or load main class com.intellij.idea.Main
  • layoutopt is missing
  • Store image to Blobstore from android client and retrieve blobkey and upload url to store in Datastore. - GAE
  • What is the difference between active scan and passive scan?
  • Get Drawable resource from inside DefaultHandler
  • 2 Solutions collect form web for “What are the common issues when migrating from ActionBarSherlock to ActionBarCompat?”

    I did some migrating and wrote down all the issues I encountered. None were serious but took time to research. I was able to migrate a quite big application in a couple hours after knowing all this. May this help to speed up migration process.

    How do I convert from ActionBarSherlock to ActionBarCompat?

    Note: Since the Support Library’s v22.1.0, the class ActionBarActivity is deprecated. You should use AppCompatActivity instead. Read here for more information: What’s the enhancement of AppCompatActivity over ActionBarActivity?

    == Switch the libraries ==

    Go to app properties and remove ActionBarSherlock and add ActionBarCompat instead. This requires the v7 appcompat library to be present, see http://developer.android.com/tools/support-library/setup.html for details. Follow the instructions precisely, ActionBarCompat needs to be a library project.

    Parallel does not work (easily) as a lot of attributes are in both libraries.

    Do not be discouraged by hundreds of errors after replacing the libraries. The vast majority goes away automatically.

    == Fix XML errors ==

    First thing is to fix all XML errors to allow compiling and find other errors.

    Replace the sherlock theme with ActionBarCompat Theme, e.g.
    <style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">

    Remove double attr, e.g. <attr name="buttonBarStyle" format="reference" />.

    For now remove all your individual action bar styles. See further down how to handle these.

    == Fix build errors ==

    Pick the easiest activities first. ActionBarCompat does not distinguish Activity and FragmentActivity, both are now ActionBarActivity.

    Remove the ActionBarSherlock imports and extend to ActionBarActivity (import android.support.v7.app.ActionBarActivity;)

    After saving, this should dramatically reduce errors in the activity.

    Fix the errors around the menues first and disregard fragment errors for now, they should be going away later.

    == Replacements ==


    • import com.actionbarsherlock.app.SherlockActivity; -> import android.support.v7.app.ActionBarActivity;
    • import com.actionbarsherlock.app.SherlockFragmentActivity; -> import android.support.v7.app.ActionBarActivity;
    • import com.actionbarsherlock.app.SherlockFragment; -> import android.support.v4.app.Fragment;
    • import com.actionbarsherlock.app.SherlockListFragment; -> import android.support.v4.app.ListFragment;
    • import com.actionbarsherlock.app.SherlockListActivity; -> import android.support.v7.app.ActionBarActivity; (see ListActivity / SherlockListActivity)
    • import com.actionbarsherlock.view.Menu; -> import android.view.Menu;
    • import com.actionbarsherlock.view.MenuItem; -> import android.view.MenuItem;
    • import com.actionbarsherlock.view.MenuInflater; -> import android.view.MenuInflater;
    • import com.actionbarsherlock.view.Window; -> import android.view.Window;
    • import com.actionbarsherlock.widget.SearchView; -> import android.support.v7.widget.SearchView;
    • import com.actionbarsherlock.widget.SearchView.OnQueryTextListener -> import android.support.v7.widget.SearchView.OnQueryTextListener;

    Code Replacements:

    • SherlockActivity -> ActionBarActivity
    • SherlockFragmentActivity -> ActionBarActivity
    • SherlockListActivity -> ListActivity (see ListActivity / SherlockListActivity)
    • SherlockListFragment -> ListFragment;

    • getSupportMenuInflater -> getMenuInflater

    • getSherlockActivity() -> getActivity()
    • com.actionbarsherlock.widget.SearchView.OnQueryTextListener() -> OnQueryTextListener (see SearchView)

    • m.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); -> MenuItemCompat.setShowAsAction(m, MenuItem.SHOW_AS_ACTION_ALWAYS);

    Typical Code changes for ActionBarCompat

    • getActionBar() -> getSupportActionBar()
    • invalidateOptionsMenu() -> supportInvalidateOptionsMenu()

    == Fragment ==

    The fragment does not cater for ActionBarCompat functionality. This is a problem when trying to call getSupportActionBar.

    This can be solved by using the onAttach method:

    public void onAttach(Activity activity) {

    Usually this is better controlled in the FragmentActivity.

    == SearchView ==

    This turned out to be a bit of a hassle.

    Replace something like this:

    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) searchItem.getActionView();


    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

    You also have to adjust your menu:

    <menu xmlns:android="http://schemas.android.com/apk/res/android" >


    <menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

    app: needs to be defined to have compatibility with android versions before 11.
    SearchView needs to be support class v7.

    == ListActivity / SherlockListActivity ==

    The ListActivity is not supported ActionBarCompat, therefore the crucial functions of the ListActivity need to be implemented manual, which is rather simple:

    private ListView mListView;
    protected ListView getListView() {
       if (mListView == null) {
        mListView = (ListView) findViewById(android.R.id.list);
       return mListView;
    protected void setListAdapter(ListAdapter adapter) {
    protected ListAdapter getListAdapter() {
       ListAdapter adapter = getListView().getAdapter();
       if (adapter instanceof HeaderViewListAdapter) {
          return ((HeaderViewListAdapter)adapter).getWrappedAdapter();
       } else {
          return adapter;

    == Styles ==

    A styled action bar can be achieved, see original google posting:

    A styled searchView box is more difficult:
    This works:

    MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
    SearchView.SearchAutoComplete theTextArea = (SearchView.SearchAutoComplete) searchView.findViewById(R.id.search_src_text);

    See these posts:

    • Changing the cursor color in SearchView without ActionBarSherlock
    • Change appcompat's SearchView text and hint color

    == Example ==

    Google Navigation Drawer with Action Bar Sherlock includes all original code (now aiming to support library) and formatting. Only some attributes had to be replaced with similar ones as they are only available from v11 onwards.

    Download at: https://github.com/GunnarBs/NavigationDrawerWithActionBarCompat

    == See also ==

    It is worth mentioning that there is no support version of the PreferenceActivity, so if you are using SherlockPreferenceActivity, you need to refactor to a support PreferenceFragment.

    More info: How to add Action Bar from support library into PreferenceActivity?

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.