0

For android programming, I have a RadioGroup which contains some RadioButtons. I want to just get the text from the button without need to get the id of the RadioButton.

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

      <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="text1"
        />

      <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="text2"
       />

      <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="text3"
        />

    </RadioGroup>

Is it possible to just get the text?

Questionerer
  • 91
  • 1
  • 11

2 Answers2

1

You can access all Views within a RadioGroup with this code:

    int count = radioGroup.getChildCount(); 
    for (int i=0;i<count;i++) {
        View radioButton = radioGroup.getChildAt(i);
        if (radioButton instanceof RadioButton) {
            Log.d(TAG,"text: "+ radioButton.getText().toString());
        }
    }

reference: Get the array of RadioButtons in a RadioGroup in Android

ASHKARAN
  • 365
  • 1
  • 13
0

I don't think its possible. Why don't you just provide id to each radiobutton ?

manjish
  • 90
  • 8