ActionBarCompat – Part 3

In the previous article we converted our resources for the code from the previous ActionBar series to use ActionBarCompat. In this article we’ll have a look at the Java code and begin looking at the changes that we need to make it backwardly compatible to API level 7 using ActionBarCompat.

The first thing that we need to remember when making the code backwardly compatible to API 7 is that there will be other classes and methods that we have used in our original code which itself is not compatible back to API 7. Specifically in the case of the ActionBar code we used Fragments, and we used the native implementations, so we’ll also need to change these to use the support library implementation of Fragment.

Let’s start simply by looking at com.stylingandroid.basicactionbar.Fragment1:

We can see from line 3 that this imports the native Fragment support which is built in to API 11 and later, so it’s a really simple task to convert this by simply changing the include:

That’s easy enough, and the change to com.stylingandroid.basicactionbar.Fragment2 is identical, so I won’t bother covering that.

Converting com.stylingandroid.basicactionbar.MainActivity is a little tricker, but it’s much more complex class than the two we’ve already converted, so that’s to be expected. The first thing to look at is the includes and identify the classes that are not available in API 7. One trick for doing this is to change the build target API level to API 7 and the compiler will identify the classes and methods which aren’t available. We’ll go through them now.

The highlighted classes are not available, so we need to change them to the equivalents from the support Library:

The next thing that we need to do is change the base class. When we use Fragments from the support library, we need to subclass FragmentActivity (or one of its siblings) instead of Activity because it provides part of the framework that the support library implementation of Fragments require. ActionBarCompat works in a similar way and requires us to subclass ActionBarActivity in any Activities where we require ActionBar support. ActionBarActivity itself subclasses FragmentActivity, so we get all of the support library Fragment framework as part of this:

The next thing that we must do is locate all calls to the getActionBar() method and replace them with getSupportActionBar(). The reason for this is that getActionBar() will exist on the class in its native form on API 11 and later, but won’t be available before then. ActionBarCompat will use the native ActionBar implementation if it is available and the back port if it is not, therefore it does not override the native getActionBar() method, ActionBarActivity provides getSupportActionBar() and we must always get a reference to the ActionBar using this method.

For example, here’s our onCreate() method:

Which we must change to:

We need to do the same thing for Fragments and replace calls to getFragmentManager() with getSupportFragmentManager().

In the next article we’ll complete the code changes necessary for our switch to ActionBarCompat.

The full source code for this article will be published along with the next article, once we have the code completed.

© 2013 – 2014, Mark Allison. All rights reserved.

CC BY-NC-SA 4.0 ActionBarCompat – Part 3 by Styling Android is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at

Leave a Reply

Your email address will not be published. Required fields are marked *