Friday, July 20, 2012

How to check the Android build version during runtime?

Suppose you are writing an application and want to make sure that min sdk version is supported by the application,  well in such a scenario you would probably specify it in the Android manifest using the following tag.
   <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="11" /> 
True, but what if you are using feature which is not essential for application and is available on certain Android API level or above? Definitely, you will enable or disable the component based on the availability. For example, the application uses the ActionBar available on Android API level 11 or above and you need to determine the Android version during runtime to enable/disable the application component using the ActionBar. The answer to all this is Build class . Build class helps to determine the Android version during run-time. Example code is shown below.

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
            ActionBar actionBar = getActionBar();
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

Related topics:
Android tutorials
How to do stuff in Android?

Your valuable comments are always welcomed. It will help to improve my post and understanding.

"By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."
By : Confucius