[Android] Get text from radioButton in radioGroup

activity_main.xml

  1.            <RadioGroup
  2.                 android:layout_width="wrap_content"
  3.                 android:layout_height="wrap_content"
  4.                 android:orientation="horizontal"
  5.                 android:layout_marginLeft="0dp"
  6.                 android:id="@+id/sell_category_radiogroup">
  7.             <RadioButton
  8.                 android:layout_width="match_parent"
  9.                 android:layout_height="match_parent"
  10.                 android:checked="false"
  11.                 android:id="@+id/sell_radio_classic"
  12.                 android:text="classic" />
  13.             <RadioButton
  14.                 android:layout_width="match_parent"
  15.                 android:layout_height="match_parent"
  16.                 android:checked="false"
  17.                 android:id="@+id/sell_radio_enduro"
  18.                 android:text="enduro" />
  19.             <RadioButton
  20.                 android:layout_width="match_parent"
  21.                 android:layout_height="match_parent"
  22.                 android:checked="false"
  23.                 android:id="@+id/sell_radio_bigbike"
  24.                 android:text="bigbike" />
  25.             <RadioButton
  26.                 android:layout_width="match_parent"
  27.                 android:layout_height="match_parent"
  28.                 android:checked="false"
  29.                 android:id="@+id/sell_radio_other"
  30.                 android:text="other" />
  31.             </RadioGroup>



MainActivity.java

  1.         RadioGroup radioGroup = (RadioGroup)findViewById(R.id.sell_category_radiogroup);
  2.         radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  3.             @Override
  4.             public void onCheckedChanged(RadioGroup group, int checkedId) {
  5.                 RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
  6.                 if (radioButton.getText().toString().equals("classic")) {
  7.                     Log.e("radio : ""classic");
  8.                 } else if (radioButton.getText().toString().equals("enduro")) {
  9.                     Log.e("radio : ""enduro");
  10.                 } else if (radioButton.getText().toString().equals("bigbike")) {
  11.                     Log.e("radio : ""bigbike");
  12.                 } else if (radioButton.getText().toString().equals("other")) {
  13.                     Log.e("radio : ""other");
  14.                 }
  15.             }
  16.         });