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.