Question

I am wondering if it is possible to use graceful degradation approach in an Android application. I.e, use some functions of, say, API 15 but if it is not supported, use API 10 instead.

Specifically, I have "swiping tabs" in Android 4 vs. missing support of this feature in Android 2.x (and thus using normal tabs) in mind but the question is rather general.

I would like to use an advanced functionality on devices that support it but when it is not supported, I would like to use an alternative. I don't seem to be able to use Android 4 libraries in an Android 2 project while an Android 4 project will not, understandably, be launchable on an Android 2 device.

Any solution? Or, at least and for this moment, any solution for "swiping tabs" on Android 2?

Was it helpful?

Solution

Part 1: To be able to use functionality for newer APIs, make sure your manifest declares your android:minSdkVersion to the lowest version (10, in your case), but set the android:targetSdkVersion to the newer version with APIs you want to use (15). In your code, you can do something like

if ( Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH ) {
    // Use the new APIs
} else {
    // Use the old APIs
}

You'll have to add an Annotation to indicate the new API usage.

Part 2: Swiping Tabs - I've used Jake Wharton's ViewPagerIndicator and the ViewPager functionality in the Android Support Library to achieve swiping tabs. It's pretty simple. There is more information at the ViewPagerIndicator page

Hope this helps.

OTHER TIPS

Make sure to use the @TargetApi directive above the class or function that only works with the newer version of the API. For example

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
void foo() {

  /* function called only on ice cream sandwich (API level 14) and greater */

}

I know you already marked your answer, but maby this could help you, its an android library called actionbar sherlock that has backported 4.x functanlities to 2.x. including fragments, pagers, tabs and the Actionbar introduced in 3.0

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top