Reputation: 16793
I have the following radigroup in the xml
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:orientation="horizontal"
android:id="@+id/radiogroup">
</RadioGroup>
and I am checking the number of objects in my json and generate radiobuttons and add them in the radiogroup as follows.
private void createRadioButton(int nImages) {
final RadioButton[] rb = new RadioButton[nImages];
for(int i=0; i<nImages; i++){
rb[i] = new RadioButton(this);
rb[i].setId(i);
radioGroup.addView(rb[i]);
}
}
now I need to know how to know and check selected radio button ?
when I was hardcoded, the following was working,
radioGroup.check(R.id.radioButton0);
but now I am adding radio buttons programmatically, I do not know how to handle
radioGroup.check(??);
Upvotes: 2
Views: 70
Reputation: 5375
If you want to select radio button at a particular position in radiogroup, you can use
radioGroup.check(radioGroup.getChildAt(position).getId());
where position is the position of the radio button in radio group
Upvotes: 2
Reputation: 508
you set id of radiobutton as index of array in this part of code rb[i].setId(i);
you can use the following code radioGroup.check(i);
Upvotes: 1