Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

xml

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.

like image 628
Ricky Zheng Avatar asked Sep 03 '12 14:09

Ricky Zheng


People also ask

How do I change the Switch size on my Android?

Using scale property to change size worked for me. Add these lines to your <switch/> tag in xml file. You can change scale value as per your need. Here value 2 makes it double in size, similarly value 0.5 makes it half in size.

How to check Switch in Android?

Important Note: We can check the current state of a Switch programmatically by using isChecked() method. This method returns a Boolean value means true or false. If a Switch is checked then it returns true otherwise it returns false. Below is an example code in which we checked the current state of a Switch.


3 Answers

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

android:checked="true" 
like image 168
Marcin Orlowski Avatar answered Oct 20 '22 13:10

Marcin Orlowski


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.

like image 5
shellym Avatar answered Oct 20 '22 13:10

shellym


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
like image 2
Ankit Shaw Avatar answered Oct 20 '22 12:10

Ankit Shaw