0

I have a radio group as you can see in the code bellow with 3 RadioButtons, my radios have an image instead of a button. So the user has to select a button by pressing one of the 3 images, and I need to check which one was selected from my Fragment.

<RadioGroup
        android:id="@+id/btnGrp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center"
        tools:layout_editor_absoluteX="30dp"
        tools:layout_editor_absoluteY="39dp" >
        <RadioButton
            android:id="@+id/btn1"
            android:button="@null"
            android:drawableRight="@drawable/btn1"
            android:background="@color/cardview_shadow_end_color"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="30dp"
            android:padding="6dip"/>
        <RadioButton
            android:id="@+id/btn2"
            android:button="@null"
            android:drawableRight="@drawable/btn2"
            android:background="@color/cardview_shadow_end_color"
            android:layout_width="wrap_content"
            android:layout_marginRight="30dp"
            android:layout_height="wrap_content"
            android:padding="6dip"/>
        <RadioButton
            android:id="@+id/btn3"
            android:button="@null"
            android:drawableRight="@drawable/btn3"
            android:background="@color/cardview_shadow_end_color"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="6dip"/>

    </RadioGroup>

This is the code related to the buttons on my fragment

     private RadioGroup radioGroup;
     private RadioButton radioButton;
     radioGroup = (RadioGroup) rootview.findViewById(R.id.btnGrp);

     int selectedId = radioGroup.getCheckedRadioButtonId();
     radioButton = (RadioButton) rootview.findViewById(selectedId);
      selectedRadioId = getResources().getResourceEntryName(radioButton.getId());

So here selectedId has a value of -1, is this because the image doesn't really behave as a RadioButton? Otherwise what is the proper way to get the checked radio button's id in my Fragment?


EDITED

Someone has mentioned in another question that putting RadioGroup inside a LinearLayout causes that RadioGroup will stop managing checked states of nested buttons and suggested using drawableRight instead of drawable, but that didn't solve my issue. That is why I am wondering if there is something wrong about the way I get the id.

Ouissal
  • 1,519
  • 2
  • 18
  • 36
  • [check this](https://stackoverflow.com/questions/38266387/how-to-make-image-as-a-radio-button-in-android) aaaand [this](https://stackoverflow.com/questions/11853245/making-radiogroup-radio-buttons-with-images-in-android-how) – Pier Giorgio Misley Jun 19 '18 at 08:07
  • You can use **setOnCheckedChangeListener** `radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if (checkedId == R.id.radioButton1) { //do work when radioButton1 is active } else if (checkedId == R.id.radioButton2) { //do work when radioButton2 is active } } });` – Rethinavel Jun 19 '18 at 08:09

3 Answers3

1

getCheckedRadioButtonId() method returns -1 if none of the RadioButtons are checked.

So you have to set one of the Radio buttons checked manually. You can do it inside xml file or Fragment. Check snippet from docummentation bellow.

/**
 * <p>Returns the identifier of the selected radio button in this group.
 * Upon empty selection, the returned value is -1.</p>
 *
 * @return the unique id of the selected radio button in this group
 *
 * @see #check(int)
 * @see #clearCheck()
 *
 * @attr ref android.R.styleable#RadioGroup_checkedButton
 */
@IdRes
public int getCheckedRadioButtonId() {
    return mCheckedId;
}
Vygintas B
  • 1,624
  • 13
  • 31
  • When setting one of them as checked, only that one's id gets returned no matter which other button I select – Ouissal Jun 19 '18 at 08:09
  • 1
    Well its because you getting id inside onCreatView. You need to set Listener for changes happening while application is running. – Vygintas B Jun 19 '18 at 08:21
1

You can use setOnCheckedChangeListener

 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        if (checkedId == R.id.radioButton1) {
         //do work when radioButton1 is active

        } else  if (checkedId == R.id.radioButton2) {
         //do work when radioButton2 is active
      }

    }
});

Please refer here.

Rethinavel
  • 3,912
  • 7
  • 28
  • 49
0

Try this,

int index = radioGroup.indexOfChild(context.findViewById(radioGroup.getCheckedRadioButtonId()));
SaravInfern
  • 3,338
  • 1
  • 20
  • 44