Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to "graceful degrade" an android application?

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?

like image 785
Malis Avatar asked Oct 25 '12 16:10

Malis


2 Answers

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.

like image 133
frenziedherring Avatar answered Nov 13 '22 18:11

frenziedherring


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 */

}
like image 29
Jason Posit Avatar answered Nov 13 '22 20:11

Jason Posit