How to set a switch in Android

I have an application that uses switches. The default value for this button is set in the main.xml file, that is:

android:id="@+id/rb_sat1E" android:checked="true" 

In the Java file, I:

 final RadioButton radio1 = (RadioButton)findViewById(R.id.rb_sat1E); 

I also created a "Reset" button in the main Java file and can use the following code to reset TextView information, i.e.

 pos1_deg.setText("0.0"); 

But how do I reset the switch? I would think that this is something like

 radio1.setBoolean("TRUE"); 

But this does not work at all.

Any help is greatly appreciated. Thank.

+45
android radio-button
Nov 09 '10 at 14:13
source share
7 answers

To use radioButton

 radio1.setChecked(true); 

It makes no sense to have only one RadioButton. If you have more, you need to disable others through

 radio2.setChecked(false); ... 

If your option is enabled, use CheckBox.

+71
Nov 09 '10 at 15:40
source share

If you want to do this in code, you can call the RadioGroup control:

 radioGroup.check(R.id.radioButtonId); 

This will check the button you specified and uncheck the others.

+74
May 30 '12 at 7:04 AM
source share

Or you can do this in an XML file:

In RadioGroup using: android:checkedButton="id_button_to_check"

or in RadioButton : android:checked="true"

+29
Dec 03 2018-11-11T00:
source share

Just to clarify this: if we have a RadioGroup with several RadioButtons and need to activate it by index, it means that:

 radioGroup.check(R.id.radioButtonId) 

and

 radioGroup.getChildAt(index)` 

We can do it:

 radioGroup.check(radioGroup.getChildAt(index).getId()); 
+7
Sep 27 '14 at 7:47
source share

if you made a design in XML format and want to show one of the flags in the group, as noted when loading the page below, the solutions can help you

  <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/txtLastNameSignUp" android:layout_margin="20dp" android:orientation="horizontal" android:id="@+id/radioGroup"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:id="@+id/Male" android:text="Male"/> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/Female" android:text="Female"/> </RadioGroup> 
+3
Mar 10 '15 at 15:05
source share

I have several RadioButtons without Group and setChecked(true) works, but setChecked(false) does not work. But this code works:

 RadioButton switcher = (RadioButton) view.findViewById(R.id.active); switcher.setOnClickListener(new RadioButton.OnClickListener(){ @Override public void onClick(View v) { if(((RadioButton)v).isSelected()){ ((RadioButton)v).setChecked(false); ((RadioButton)v).setSelected(false); } else { ((RadioButton)v).setChecked(true); ((RadioButton)v).setSelected(true); } } }); 
0
Nov 17 '17 at 11:33
source share
 btnDisplay.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // get selected radio button from radioGroup int selectedId = radioSexGroup.getCheckedRadioButtonId(); // find the radiobutton by returned id radioSexButton = (RadioButton) findViewById(selectedId); Toast.makeText(MyAndroidAppActivity.this, radioSexButton.getText(), Toast.LENGTH_SHORT).show(); } }); 
-one
May 10 '16 at 11:00
source share



All Articles