For the application I am currently developing, I need to adapt the layout of the different activities to the user\'s Android API level.
Is there a way to do this?
If you already have drawable resources for each of the platform level, you can use the information provided in http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources
More specifically, look at the 'Platform Version (API Level)' row in Table 2.
As Andrew Koester said you can use the different version folders, but I found this to be a lot of work because it would not fall back to the default layout. If you used layout-v14, it will work,but any api after 14 will also have this layout and you must use another layout-v? to override it again. It all depends on what your doing, but I found if your doing a lot of stuff programmatically this works wonders:
if(Build.VERSION.SDK_INT == Build.VERSION_CODES.ICE_CREAM_SANDWICH || Build.VERSION.SDK_INT == Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1){
//ex. if ics is met then do this
}else{
//if api is not ics then do this
}
If what you're trying to do is show a different layout depending on which API version is available on the device, you want to use configuration qualifiers. The specifics for alternative resources are also documented.
The most basic way to do it is to create a layout folder for each API level you want to use, formatted as follows:
res/layout/mylayout.xml (Default)
res/layout-v4/mylayout.xml (Android 1.6)
res/layout-v11/mylayout.xml (Android 3.0)
and so on, where vN
is the API level. The specific API levels can be found on this page.