How to set a radio button in Android
Asked Answered
C

9

75

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.

Caracole answered 9/11, 2010 at 14:13 Comment(0)
S
113

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.

Smelly answered 9/11, 2010 at 15:40 Comment(2)
For other readers who may find themselves in my position: setChecked (true), is COMPLETELY DIFFERENT from setSelected (true). Ack.Beagle
This doesn't work if radio buttons placed in radio group so use answer given by gulchriderShrine
C
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.

Clump answered 30/5, 2012 at 7:4 Comment(3)
Thanks :) This is the better answer for me; exactly what I was looking for.Coenocyte
radioGroup.check(radioGroup.getChildAt(0).getId()); - to check first item on listTsosie
I agree. Please do that.Smelly
P
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"

Poignant answered 3/12, 2011 at 15:21 Comment(3)
When I check radiobutton in xml, I cant uncheck it later. It's like that radiobutton is removed from the groupGallardo
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 radiogroupCarom
we can clear the selection by calling check(-1) or clearCheck().Shiite
R
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());
Reimport answered 27/9, 2014 at 7:47 Comment(0)
L
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>
Litt answered 10/3, 2015 at 15:5 Comment(0)
C
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)

Crop answered 2/1, 2018 at 10:23 Comment(0)
E
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

Epicureanism answered 12/9, 2020 at 16:17 Comment(0)
A
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);
                                }
                            }

                        });
Attrahent answered 17/11, 2017 at 11:33 Comment(0)
A
-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();

    }

});
Alesandrini answered 10/5, 2016 at 11:0 Comment(1)
Can you please explain why OP should use your code?Coerce

© 2022 - 2024 — McMap. All rights reserved.