How to set the default value of the switch widget in android

Solution 1:

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

android:checked="true" 

Solution 2:

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.

Solution 3:

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