59

I am coding my xml file with the couple switch widgets. but I don't know how to set the default value of them as ON. And I just want to get this problem solved within the xml file so no Java.

3

3 Answers 3

125

To make Switch set "ON" in you XML file use this:

android:checked="true" 
1
  • Value is still false when I run my app when I include this in XML?
    – grantespo
    Jan 19, 2017 at 3:34
4

To set the Switch default as ON in XML file :

android:checked = "true"

Don't try using android:pressed = "true" . It will throw an error while running the app.

1

You can set the Switch default as ON/OFF from the code itself. Try something like this:

    switch.setChecked(true) // to set it true

Or,

    switch.setChecked(false) // to set it false

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.