Search code examples
javaandroidxmlradio-buttonandroid-radiogroup

Display button text from from selected index in a RadioGroup


I have my app auto-populate an email with info the user entered via a form. When i try to display their selected index it gives me

android.support.v7.widget.AppCompatRadioButton {2d57753c VFED..C. ...P..ID 705,0-903,96#7f0d0056 app:id/RejectRadio}

This is the correct id and such but I would rather it say just "Reject", or if the other button is selected just "Accept". How would I do this.

XML

<RadioGroup
   android:id="@+id/radiochoice"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_below="@+id/EditTextFeedbackBody"
   android:orientation="horizontal">

        <RadioButton
            android:id="@+id/AcceptRadio"
            android:layout_width="wrap_content"
            android:layout_marginStart="50dp"
            android:layout_height="wrap_content"
            android:text="@string/acceptbutton" />

        <RadioButton
            android:id="@+id/RejectRadio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/rejectbutton"
            android:layout_marginStart="115dp" />
</RadioGroup>

JAVA

int radioButtonID = radiochoice.getCheckedRadioButtonId();
View radioButton = radiochoice.findViewById(radioButtonID);
int index = radiochoice.indexOfChild(radioButton);

final RadioGroup ApproveorReject = (RadioGroup) findViewById(R.id.radiochoice);
fAnswer = ApproveorReject.getChildAt(index).toString();

Solution

  • Are you trying to get the text of the radio button?

     RadioButton btn = (RadioButton) ApproveorReject.getChildAt(index);
     fAnswer  = (String) btn.getText();