11

I want to use the actionbar feature included in SDK 11. However I also want the app to run on earlier devices from SDK 10 (2.3.3). I am willing to give up the actionbar feature for the earlier devices as it is not an important feature. I have done all the reading about reflection, wrapper class and some other techniques. I am now stumped on exactly how to make this work. I am using Eclipse.

If I don't set the target in Eclipse to sdk 11 or greater, then any place I have a reference to actionBar gives a compile error. If I put the target to sdk 11 or greater it compiles but won't show that it can run on earlier devices. I have android:minSdkVersion=10 set all the time.

Can someone give me some insight on how to make the references to actionBar and yet get it to target a previous sdk level? Thanks in advance.

Nathan Campos
  • 28,769
  • 59
  • 194
  • 300
Robert Grimes
  • 121
  • 1
  • 4

1 Answers1

18

Yes! You can definitely do this. Try following the pattern outlined below.

In your AndroidManifest.xml file declare the following (replacing the platform versions with whatever your app requires):

<!-- Build Target -->
<uses-sdk android:targetSdkVersion="14" android:minSdkVersion="7" />

By targeting a platform version of API 11 or higher, you are allowing Eclipse to link (compile) against the native ActionBar classes. Providing an earlier minimum platform version allows your app to be installed (run) on older versions of Android.

Your Activity code should then look something like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (CompatibilityManager.isHoneycomb()) {
        final ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowHomeEnabled(true);
        // ...
    } else {
        // The ActionBar is unavailable!
        // ...
    }
}

Where the CompatibilityManager.java class simply provides static helper methods for determining the current version of the SDK:

public class CompatibilityManager {
    public static final String KINDLE_FIRE_MODEL = "Kindle Fire";

    /**
     * Get the current Android API level.
     */
    public static int getSdkVersion() {
        return android.os.Build.VERSION.SDK_INT;
    }

    /**
     * Determine if the device is running API level 11 or higher.
     */
    public static boolean isHoneycomb() {
        return getSdkVersion() >= Build.VERSION_CODES.HONEYCOMB;
    }

    /**
     * Determine if the device is running API level 14 or higher.
     */
    public static boolean isIceCreamSandwich() {
        return getSdkVersion() >= Build.VERSION_CODES.ICE_CREAM_SANDWICH;
    }

    /**
     * Determine if the current device is a first generation Kindle Fire.
     * @return true if the device model is equal to "Kindle Fire", false if otherwise.
     */
    public static boolean isKindleFire() {
        return Build.MODEL.equals(KINDLE_FIRE_MODEL);
    }
}

You might also consider leveraging the ActionBarSherlock library, which provides a compatible ActionBar API all the way back to Android 2.x:

The library will automatically use the native action bar when available or will automatically wrap a custom implementation around your layouts. This allows you to easily develop an application with an action bar for every version of Android back through 2.x.

Have fun!

twaddington
  • 11,607
  • 5
  • 35
  • 46
  • 1
    Further documentation on general backward compatibility can be found at http://developer.android.com/resources/articles/backward-compatibility.html The action bar documentation covers this as well, and how to use it in a basic form and still be backward compatible. http://developer.android.com/guide/topics/ui/actionbar.html#Adding – cistearns Mar 06 '12 at 07:46
  • Ok, I did some similar coding. I will try you suggestion as well. The problem I then found was that when I tried to run the app and the emulator (in eclipse) came up, only the target level devices appeared. I finially used AVD manager to start the lower level device and then specifically used it while it was running. I choose not to use ActionBarSherlock since I only want the feature in new devices for now. I might change my mind later. Thanks. – Robert Grimes Mar 07 '12 at 17:30
  • 2
    After changing the build target and min version in the AndroidManifest.xml you might need to tell Eclipse that the build target was changed. To do so, you can right-click on your project and select `Properties`. Then select `Android` in the left menu and ensure the target that is checked is equal to the one specified in your manifest. All this does is update the `project.properties` build file in your project root directory. – twaddington Mar 07 '12 at 18:45
  • 1
    "By targeting a platform version of API 11 or higher, you are allowing Eclipse to link (compile) against the native ActionBar classes." -- not that this has nothing to do with the `` element. – CommonsWare Jan 18 '13 at 23:01