How to set a radio button in Android


Question

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.

1
53
9/20/2014 10:05:59 AM

Accepted Answer

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.

89
11/9/2010 3:40:35 PM

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.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon