Set the selected Android RadioGroup index

Is there a way to set the selected RadioGroup index in android, besides looping through the child radio buttons and selecting the check switch on the selected index?

Note. I populate the Radio Button group at runtime.

+81
android android-radiogroup android-radiobutton
Mar 23 2018-12-12T00:
source share
5 answers

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

radioGroup.check(R.id.myButtonId); 

If you created your radio group programmatically (I don’t even know how you do it ...), you may need to create a special XML layout file only for a group of radio stations so that you can assign R.id. * ids to buttons.

Please see the answer below, if you really want to set a group of radio buttons by index, see the answer below.

+181
Mar 23 2018-12-12T00:
source share

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

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

........

Additional Information: It seems that Google wants you to use an identifier instead of an index, because using an identifier is more reliable. For example, if you have another user interface element in your RadioGroup, or if another developer re-orders RadioButtons, the indexes may change and not be what you expected. But if you are the only developer, everything is in order.

+85
Feb 14 '15 at 23:12
source share

The answer to Siavash is correct:

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

But keep in mind that a radioGroup may contain views other than radioButtons - like this example, which includes a small line under each selection.

 <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 index 1, for example, will result in an error. The entry in index 1 is the first dividing line, not the radio beater. The radio bytes in this example are at indices 0, 2, 4, 6.

+5
Jul 19 '16 at 2:53 on
source share

you can make findViewById from the radio group.

 ((RadioButton)my_radio_group.findViewById(R.id.radiobtn_veg)).setChecked(true);` 
+2
Sep 09 '15 at 5:03
source share

This worked for me, I dynamically created a switch using

  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 select the button using

 int radio_button_Id = radioGroup.getChildAt(index).getId(); radioGroup.check( radio_button_Id ); 
+1
Nov 15 '16 at 11:19
source share



All Articles