74

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.

appukrb
  • 1,507
  • 4
  • 24
  • 53
Entropy1024
  • 7,847
  • 9
  • 30
  • 32

9 Answers9

112

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.

Zelimir
  • 11,008
  • 6
  • 50
  • 45
  • 15
    For other readers who may find themselves in my position: setChecked (true), is COMPLETELY DIFFERENT from setSelected (true). Ack. – Rich Jul 08 '13 at 16:02
  • 1
    This doesn't work if radio buttons placed in radio group so use answer given by gulchrider – Shriram Panchal Apr 03 '23 at 07:30
108

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.

gulchrider
  • 4,306
  • 3
  • 25
  • 16
34

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"

Samoht
  • 489
  • 4
  • 8
  • 2
    When I check radiobutton in xml, I cant uncheck it later. It's like that radiobutton is removed from the group – shift66 Oct 19 '15 at 19:05
  • sorry that I'm late, but this is the intended behaviour for radiobuttons. When you select it, you can't unselect it EXCEPT by selecting another radio button in the radiogroup – techfly Oct 04 '16 at 11:44
  • we can clear the selection by calling check(-1) or clearCheck(). – Al Mamun Feb 23 '18 at 04:09
13

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());
Yurii
  • 4,811
  • 7
  • 32
  • 41
cvyryk
  • 131
  • 1
  • 2
5

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>
Nirmal Dhara
  • 2,121
  • 18
  • 27
3

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)

King Ukah
  • 13
  • 1
  • 7
anoop ghildiyal
  • 821
  • 10
  • 18
2

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

Jeevan Rupacha
  • 3,055
  • 1
  • 15
  • 29
0

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);
                                }
                            }

                        });
Eugen
  • 1,356
  • 12
  • 15
-1
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();

    }

});
Tunaki
  • 132,869
  • 46
  • 340
  • 423