How to set a radio button in Android

Entropy1024 picture Entropy1024 · Nov 9, 2010 · Viewed 135.3k times · Source

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.

Answer

Zelimir picture Zelimir · Nov 9, 2010

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.