Implementing (switch\toggle button) depending on API level

1k views Asked by At

My application will run on api level 9 devices or higher. Now i have a toggle_button on one of the layouts. I want to change this toggle button to switcher if the device is apilevel 14 or higher. How can i implement this? Sorry for my english.

2

There are 2 answers

4
Toumash On BEST ANSWER

If you are creating the content/pages in xml, simply copy your xml with same name but to the layout-v14 folder. Then change ToggleButton to SwitchButton, and Android will take care of rest :)
If you are coding it in jave, use:

if(Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH){
    //Add Switch
}else{
    //add toggle
}

And when you'll want to use both (when using defining in xml) with the same java code use:

CompoundButton bt_toggle= (CompoundButton) findViewById(R.id.some_button);
bt_toggle.setChecked(false);
0
Valentin Baryshev On

I have found a solution! link

It just adds a support library. And you can use switchers from api-level7!