72

I create a RadioGroup from XML

    <RadioGroup android:id="@+id/option" 
        android:layout_width="match_parent"
        android:orientation="horizontal" 
        android:checkedButton="@+id/block_scenario_off"
        android:layout_height="wrap_content">
        <RadioButton 
            android:layout_width="0dip"
            android:layout_weight="1" 
            android:text="@string/option1" 
            android:layout_height="wrap_content" 
            android:id="@+id/option1"
            android:layout_gravity="center|left" 
            android:onClick="@string/on_click"/>
        <RadioButton 
            android:layout_width="0dip"
            android:layout_weight="1" 
            android:text="@string/option2" 
            android:onClick="@string/on_click"
            android:layout_height="wrap_content"
            android:layout_gravity="center" 
            android:id="@+id/option2"/>
        <RadioButton 
            android:layout_width="0dip"
            android:layout_weight="1" 
            android:text="@string/option3"
            android:onClick="@string/on_click" 
            android:layout_height="wrap_content"
            android:layout_gravity="center|right" 
            android:id="@+id/option3" />
    </RadioGroup>

In Java code, I programmatically check the first one on activity creation (onCreate()) as following:

    mOption = (RadioGroup) findViewById(R.id.option);
    mOption.check(R.id.option1);

But when the activity is shown, no radio button is checked. Any help?

Bao Le
  • 16,643
  • 9
  • 65
  • 68
  • 2
    If you wanted to check the radio buttons programmatically then you should not have accepted that answer because that is how we set them checked on the xml file! – arniotaki Jun 04 '14 at 10:35

12 Answers12

87

In your layout you can add android:checked="true" to CheckBox you want to be selected.

Or programmatically, you can use the setChecked method defined in the checkable interface:

RadioButton b = (RadioButton) findViewById(R.id.option1); b.setChecked(true);

EvilTeach
  • 28,120
  • 21
  • 85
  • 141
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • Try to add that option to both RadioGroup and/or RadioButton levels, does not work – Bao Le Oct 12 '11 at 16:44
  • you have to add this option ONLY to RadioButton checked by default – Blackbelt Oct 12 '11 at 16:45
  • 7
    Yes, it works for the RadioButton with checked attribute assigned in XML, but it does not work with Java code (i.e. mOption.check(R.id.option1)); – Bao Le Oct 12 '11 at 17:02
  • 2
    Since I have two RadioGroup in the same layout, the code does not work. If I remove one, the other works well. I have still the problem with two or more RadioGroup in the same layout. Thanks – Bao Le Oct 13 '11 at 01:02
  • This is NOT what "programmatically" means. – Rodrigo Dec 30 '14 at 04:19
  • 1
    This answer is wrong and dangerous. setting a radiobutton checked will not modify the state of the RadioGroup. Bao Le is saying himself why he wasn't able to make it work: he had the same id repeated twice inside the xml. – AndrewBloom May 08 '16 at 14:01
  • @AndrewBloom would you mind explaining what's *dangerous and wrong* in this answer ? – Blackbelt May 08 '16 at 14:03
  • 1
    @Blackbelt the fact that you see the radiobutton set (after you called setChecked), but the state of the RadioGroup is not changed inside. This could lead to subtle bugs where you see a radiobutton set visually but you query the state of the RadioGroup and you get the id of another radiobutton set. – AndrewBloom May 08 '16 at 15:11
  • @Blackbelt i just had a bug where i setChecked(false) a radiobutton, so the RadioGroup was all unchecked, and clicking on the same option didn't turn the radiobutton on (because for the radiogroup it was already on). So wrong is the fact that doesn't change the RadioGroup state, which is the object that should manage the state. Dangerous is cause it leads to subtle bugs. – AndrewBloom May 08 '16 at 15:16
  • 1
    @AndrewBloom, it is not a subtle bug. It is the expected behavior as per RadioGroup documentation. – Blackbelt May 08 '16 at 15:25
  • @Blackbelt, it's difficult to explain to a user that sees no options set on a radiogroup but he can't set one on of them clicking on it, that is 'the expected behaviour as per RadioGroup documentation' though. – AndrewBloom May 08 '16 at 15:52
  • Thanks bro, this helped me so much! – Reza Hamzehei Nov 11 '17 at 15:12
  • Downvoted because this answer doesn't actually result in expected behaviour: to wit, that the radiobutton will behave as if selected by the user; and other radiobuttons in the group will become deselected. Nobody uses a single radiobutton in isolation, so this answer is at best useless and at worst harmful. – Sod Almighty Oct 01 '22 at 10:51
41

try this if you want your radio button to be checked based on value of some variable e.g. "genderStr" then you can use following code snippet

    if(genderStr.equals("Male"))
       genderRG.check(R.id.maleRB);
    else 
       genderRG.check(R.id.femaleRB);
shraddha
  • 309
  • 1
  • 2
  • 13
Shashank Degloorkar
  • 3,151
  • 4
  • 32
  • 50
32

I use this code piece while working with indexes for radio group:

radioGroup.check(radioGroup.getChildAt(index).getId());
Seref Bulbul
  • 1,163
  • 12
  • 14
30

Watch out! checking the radiobutton with setChecked() is not changing the state inside the RadioGroup. For example this method from the radioGroup will return a wrong result: getCheckedRadioButtonId().

Check the radioGroup always with

mOption.check(R.id.option1);

you've been warned ;)

AndrewBloom
  • 2,171
  • 20
  • 30
27

You may need to declare the radio buttons in the onCreate method of your code and use them.

RadioButton rb1 = (RadioButton) findViewById(R.id.option1);
rb1.setChecked(true);
vik
  • 324
  • 1
  • 2
  • 5
  • 1
    This isn't working for me, nor mOption.check(R.id.option1); I have tried in onCreate() and onResume(). What is wrong? – Rodrigo Dec 30 '14 at 04:21
14

Also you can use getChildAt() method. Like this:

mOption = (RadioGroup) findViewById(R.id.option);
((RadioButton)mOption.getChildAt(0)).setChecked(true);
Taras Lozovyi
  • 876
  • 9
  • 15
13

Grab the radio group and look at the children to see if any are unchecked.

RadioGroup rg = (RadioGroup) view;
int checked = savedInstanceState.getInt(wrap.getAttributeName());

if(checked != -1) {
    RadioButton btn = (RadioButton) rg.getChildAt(checked);
    btn.toggle();
}
JPM
  • 9,077
  • 13
  • 78
  • 137
Mateusz Mazur
  • 131
  • 1
  • 3
13

I use this code piece while working with getId() for radio group:

radiogroup.check(radiogroup.getChildAt(0).getId());

set position as per your design of RadioGroup

hope this will help you!

8

it will work if you put your mOption.check(R.id.option1); into onAttachedToWindow method or like this:

view.post(new Runnable()
{
    public void run() 
    {
        // TODO Auto-generated method stub
        mOption.check(R.id.option1); 
    }
});

the reason may be that check method will only work when the radiogroup is actually rendered.

Jake1164
  • 12,291
  • 6
  • 47
  • 64
david
  • 81
  • 1
  • 1
  • david's comment in his answer that the check method would not work until the radiogroup was actually rendered turned out to solve a mystery for me. – Alyoshak Sep 20 '18 at 22:24
5

In Kotlin. Select first element. And you need to do this in onViewCreated or later.

radioGroup.apply { check(getChildAt(0).id) }
kulikovman
  • 333
  • 4
  • 8
2

I prefer to use

RadioButton b = (RadioButton) findViewById(R.id.option1);
b.performClick();

instead of using the accepted answer.

RadioButton b = (RadioButton) findViewById(R.id.option1);
b.setChecked(true);

The reason is setChecked(true) only changes the checked state of radio button. It does not trigger the onClick() method added to your radio button. Sometimes this might waste your time to debug why the action related to onClick() not working.

Codingpan
  • 318
  • 2
  • 6
0

For Kotlin:

check(radioGroup.children.first().id)

M.Muzammil
  • 643
  • 9
  • 18