Using Newer APIs in Your Android App Development

By Michael Burton

Every version of Android introduces some new APIs. For example, Android 5.0 introduced the new Toolbar API. To use the Toolbar, the AppCompat library provides an alternative version of Toolbar that works on older versions of Android.

But what do you do if you have no equivalent for a new API in AppCompat? After all, AppCompat can’t be expected to provide ports of new functionality for every single old version of Android.

In cases where a new API isn’t available on older versions of Android, and you have no support for it in the AppCompat or other Android support libraries, you must disable that functionality in your app when it is run on versions of Android that do not support that feature.

The way to do this is to check the version of Android before you attempt to use one of these APIs. If you’re running on a version of Android that is too old, then disable that feature; otherwise, let it go through. For example, if your app uses the new Advanced Camera APIs introduced with Android 5.0, you could do something like the following:

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ) {
        String[] ids = cameraManager.getCameraIdList();
        . . .
    } else {
        Toast.makeText(this,
            "Sorry, that feature is not available on this " +
                "version of Android",
            Toast.LENGTH_SHORT).show();
    }

You won’t need to do this for any of the APIs that the Tasks app uses, but it’s good to know what to do should you ever need it.