107

Is there a way to set the selected index of a RadioGroup in android, other than looping through the child radiobuttons and selecting checking the radio button at the selected index?

Note: I am populating the Radio Button Group at run time.

Jonik
  • 80,077
  • 70
  • 264
  • 372
Hassan Mokdad
  • 5,832
  • 18
  • 55
  • 90

7 Answers7

229

If your radio group is defined in a layout xml file, each button can be assigned an id. Then you just check a button like this

radioGroup.check(R.id.myButtonId);

If you created your radio group programmatically (I'm not even sure how you do this...), you might want to consider creating a special layout xml file just for the radio group so that you can assign R.id.* ids to the buttons.

Please see the answer below if you are, in fact, looking to set the radio button group by index, see the answer below.

((RadioButton)radioGroup.getChildAt(index)).setChecked(true);
Naveed Ahmad
  • 6,627
  • 2
  • 58
  • 83
jjm
  • 6,028
  • 2
  • 24
  • 27
  • 2
    Hi, I created them programtically using this code:for (int i = 0; i < lookupTypes.size(); i++) { // RadioButton rbt = new RadioButton(this);//, null, // R.style.RadioGroupItem); RadioButton rbt = (RadioButton) getLayoutInflater().inflate( R.layout.tmpltradiobutton, null); rbt.setId(i); rbt.setText(lookupTypes.get(i).getValue()); rbt.setTag(lookupTypes.get(i)); rbtnGroup.addView(rbt); } – Hassan Mokdad Mar 23 '12 at 16:11
  • Since you're setting their ids to the index in lookupTypes, you can just use that index as the argument to the `check` method. – jjm Mar 23 '12 at 16:20
  • this answer literally just explains again what the question asker already stated - the other answer should be the accepted answer since it correctly tells how to select by the index. – Lassi Kinnunen Mar 13 '15 at 08:14
  • @LassiKinnunen I feel like the number of upvotes on this answer suggests that people are getting what they need from it...that said, you are right that the other answer is more correct for the question asked :-/ – jjm Mar 13 '15 at 19:04
  • 2
    Honestly I feel a bit embarrassed that this is my highest voted SO answer. – jjm Mar 16 '15 at 20:46
  • hi, when i use this code lane ,it doesn't trigger onCheckedChanged() function , do you know ? why ? @jjm – ozanurkan Jan 16 '20 at 14:02
92

Question said "set selected INDEX", here's how to set it by index:

((RadioButton)radioGroup.getChildAt(index)).setChecked(true);

........

Additional info: It seems that Google wants you to use id instead of index, because using id is more bug proof. For example, if you have another UI element in your RadioGroup, or if another developer re-orders the RadioButtons, the indices might change and not be what you expected. But if you're the only developer, this is totally fine.

Siavash
  • 7,583
  • 13
  • 49
  • 69
  • Can you explain this code, how it is the right answer? – rfornal Feb 15 '15 at 00:12
  • 1
    @rfornal this code allows you to grab the radio button by INDEX, as opposed to by View id (such as R.id.radioButton1), and reduces the need to implement a lookup table to convert index to view id. – Siavash Feb 17 '15 at 03:51
7

you can do findViewById from the radio group .

((RadioButton)my_radio_group.findViewById(R.id.radiobtn_veg)).setChecked(true);`
Faheem
  • 930
  • 10
  • 7
5

Siavash's answer is correct:

((RadioButton)radioGroup.getChildAt(index)).setChecked(true);

But be aware that a radioGroup can contain views other than radioButtons -- like this example that includes a faint line under each choice.

<RadioGroup
    android:id="@+id/radioKb"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/kb1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:button="@null"
        android:drawableRight="@android:drawable/btn_radio"
        android:text="Onscreen - ABC" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#33000000" />

    <RadioButton
        android:id="@+id/kb2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:button="@null"
        android:drawableRight="@android:drawable/btn_radio"
        android:text="Onscreen - Qwerty" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#33000000" />

    <RadioButton
        android:id="@+id/kb3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:button="@null"
        android:drawableRight="@android:drawable/btn_radio"
        android:text="Standard softkey" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#33000000" />

    <RadioButton
        android:id="@+id/kb4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:button="@null"
        android:drawableRight="@android:drawable/btn_radio"
        android:text="Physical keyboard" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#33000000" />

</RadioGroup>

In this case using an index of 1, for example, would generate an error. The item at index 1 is the first separator line -- not a radioButton. The radioButtons in this example are at indexes 0, 2, 4, 6.

Alan
  • 325
  • 2
  • 5
2

This Worked For me, I created radio button dynamically by

  private void createRadioButton() {

    RadioButton[] rb = new RadioButton[5];

    RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            1.0f);

    radioGroup.setOrientation(RadioGroup.HORIZONTAL);

    for (int ID = 0; ID < 5; ID++) {
        rb[ID] = new RadioButton(this);
        rb[ID].setLayoutParams(layoutParams);
        rb[ID].setText("Button_Text");
        radioGroup.addView(rb[ID]); //the RadioButtons are added to the radioGroup instead of the layout
    }
}

Now Check a button using,

int radio_button_Id = radioGroup.getChildAt(index).getId();

radioGroup.check( radio_button_Id );
Mukeshkumar S
  • 785
  • 1
  • 14
  • 30
0

Inside onBindViewHolder set the tag to Button Group

@Override
public void onBindViewHolder(final CustomViewHolder holder, final int position) {
   ...
   holder.ButtonGroup.setTag(position);
}

and in the ViewHolder

ButtonGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        ...
        int id = radioGroup.getCheckedRadioButtonId();

        RadioButton radioButton = (RadioButton) radioGroup.findViewById(id);

        int clickedPos = (Integer) radioGroup.getTag();

        packageModelList.get(clickedPos).getPoll_quest()
    }
pushkin
  • 9,575
  • 15
  • 51
  • 95
Jiten Basnet
  • 1,623
  • 15
  • 31
0

Using Kotlin you can make it by

  (radio_group_id.getChildAt(index) as RadioButton).isChecked = true

or

radio_group_id.check(R.id.radio_button_id)

Mohamed AbdelraZek
  • 2,503
  • 4
  • 25
  • 36