Search code examples
androidandroid-switchandroid-togglebutton

Implementing (switch\toggle button) depending on API level


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.


Solution

  • 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);