Bao Le
Bao Le

Reputation: 17497

RadioGroup: How to check programmatically

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?

Upvotes: 73

Views: 129080

Answers (12)

M.Muzammil
M.Muzammil

Reputation: 683

For Kotlin:

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

Upvotes: 0

kulikovman
kulikovman

Reputation: 391

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

radioGroup.apply { check(getChildAt(0).id) }

Upvotes: 5

Codingpan
Codingpan

Reputation: 318

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.

Upvotes: 2

Saurabh Gaddelpalliwar
Saurabh Gaddelpalliwar

Reputation: 1645

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!

Upvotes: 13

Seref Bulbul
Seref Bulbul

Reputation: 1173

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

radioGroup.check(radioGroup.getChildAt(index).getId());

Upvotes: 32

AndrewBloom
AndrewBloom

Reputation: 2408

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

Upvotes: 30

Taras Lozovyi
Taras Lozovyi

Reputation: 926

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

mOption = (RadioGroup) findViewById(R.id.option);
((RadioButton)mOption.getChildAt(0)).setChecked(true);

Upvotes: 14

Blackbelt
Blackbelt

Reputation: 157447

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

Upvotes: 87

Shashank Degloorkar
Shashank Degloorkar

Reputation: 3221

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

Upvotes: 42

david
david

Reputation: 81

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.

Upvotes: 8

Mateusz Mazur
Mateusz Mazur

Reputation: 131

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

Upvotes: 13

vik
vik

Reputation: 324

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

Upvotes: 27

Related Questions