How to set a radio button in Android
I have an app that uses radio buttons. The default for this button is set in the main.xml file, ie:
android:id="@+id/rb_sat1E"
android:checked="true"
In the Java file I have:
final RadioButton radio1 = (RadioButton)findViewById(R.id.rb_sat1E);
I have also created a 'Reset' button in the main Java file and can use the following code to reset TextView information ie.
pos1_deg.setText("0.0");
But how do I reset a radio button? I would have thought it to be something like
radio1.setBoolean("TRUE");
But that does not work at all.
Any help greatly appreciated. Thanks.
Solution 1:
For radioButton use
radio1.setChecked(true);
It does not make sense to have just one RadioButton. If you have more of them you need to uncheck others through
radio2.setChecked(false); ...
If your setting is just on/off use CheckBox.
Solution 2:
If you want to do it in code, you can call the check member of RadioGroup:
radioGroup.check(R.id.radioButtonId);
This will check the button you specify and uncheck the others.
Solution 3:
Or you can do it in the XML file :
In the RadioGroup using : android:checkedButton="id_button_to_check"
or in the RadioButton : android:checked="true"