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

AndroidXml

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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

android:checked="true" 

Solution 2 - Android

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 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRicky ZhengView Question on Stackoverflow
Solution 1 - AndroidMarcin OrlowskiView Answer on Stackoverflow
Solution 2 - AndroidshellymView Answer on Stackoverflow
Solution 3 - AndroidAnkit ShawView Answer on Stackoverflow