Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Using Switch Preference pre API level 14

Pre-API Level 14 there is no switch preference. If I use preferences.xml to create my preference screen is there some way to distinguish between the API levels? So having a check box for old releases and a switch for API 14?

What would be the best way?

like image 507
AndyAndroid Avatar asked Sep 11 '25 10:09

AndyAndroid


2 Answers

If I use preferences.xml to create my preference screen is there some way to distinguish between the API levels? So having a check box for old releases and a switch for API 14?

Create a res/xml-v14/ directory that contains a preferences.xml that has your SwitchPreference. Create a res/xml/ directory that contains a preferences.xml file that replaces the SwitchPreference with a CheckBoxPreference. Android will load the right preferences.xml file edition based on the device version the app is running on.

like image 87
CommonsWare Avatar answered Sep 13 '25 00:09

CommonsWare


You could also use the android-switch-backport library which has a SwitchPreference which works on Android 2.1+.

https://github.com/BoD/android-switch-backport

like image 31
Intrications Avatar answered Sep 12 '25 23:09

Intrications