How to set a radio button in Android
AndroidRadio ButtonAndroid Problem Overview
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.
Android Solutions
Solution 1 - Android
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 - Android
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 - Android
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"
Solution 4 - Android
Just to clarify this: if we have a RadioGroup
with several RadioButtons
and need to activate one by index, implies that:
radioGroup.check(R.id.radioButtonId)
and
radioGroup.getChildAt(index)`
We can to do:
radioGroup.check(radioGroup.getChildAt(index).getId());
Solution 5 - Android
if you have done the design in XML and want to show one of the checkbox in the group as checked when loading the page below 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>
Solution 6 - Android
Many times if your radio buttons belong to the same radioGroup then
radioButton.setChecked(true)
will not select the radio button properly. So to solve this problem try using your radioGroup.
radioGroup.check(R.id.radioButtonId)
Solution 7 - Android
Use this code
((RadioButton)findViewById(R.id.radio3)).setChecked(true);
mistake -> don't forget to give () for whole before setChecked() -> If u forget to do that setChecked() is not available for this radio button
Solution 8 - Android
I have multiple RadioButtons without Group and setChecked(true)
works, but setChecked(false)
don't works. 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);
}
}
});
Solution 9 - Android
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();
}
});