Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adapt layout to Android platform or API level

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?

like image 382
Atheh Avatar asked Jun 30 '11 14:06

Atheh


People also ask

What API level should I develop for Android?

New apps must target Android 12 (API level 31) or higher; except for Wear OS apps, which must target Android 11 (API level 30) or higher.

What is API and API level in Android?

API Level is an integer value that uniquely identifies the framework API revision offered by a version of the Android platform. The Android platform provides a framework API that applications can use to interact with the underlying Android system. The framework API consists of: A core set of packages and classes.

How do I change the API level in Android project?

Step 1: Open your project in Android mode then go to Gradle Scripts > build. gradle(Module: app) as shown in the following image. Step 2: Refer to the below image and here you have to change the minSdkVersion and targetSdkVersion as per the requirement.


2 Answers

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.

like image 157
Jess Avatar answered Oct 12 '22 01:10

Jess


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         } 
like image 28
jeff darnell Avatar answered Oct 12 '22 02:10

jeff darnell