I am trying to create a check-mark in a RadioButton
that disappears if that button is not selected. However, the custom check-mark only seems to react to state_checked
if I put it in the android:button
attribute of RadioButton
. This is unexpected, because if I replace the custom check-mark by android:drawableRight="?android:attr/listChoiceIndicatorSingle"
, this works perfectly fine. Including the check-mark in drawableRight
has my preference since setting the spacing on the android:button
attribute is problematic.
Are there different settings that will make the check-mark behave as expected?
This is the current code:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:button="@null"
android:drawableRight="@drawable/period_selector"
android:background="?android:selectableItemBackground"
android:layoutDirection="rtl"
android:layout_gravity="start"
android:textAlignment="textStart"
android:paddingVertical="16dp"
android:paddingHorizontal="16dp"
android:text="@string/period_selection_month"
android:textSize="16sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?divider" />
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:button="@null"
android:drawableRight="@drawable/period_selector"
android:background="?android:selectableItemBackground"
android:layoutDirection="rtl"
android:layout_gravity="start"
android:textAlignment="textStart"
android:paddingVertical="16dp"
android:paddingHorizontal="16dp"
android:text="@string/period_selection_year"
android:textSize="16sp" />
</RadioGroup>
with drawable/period_selector.xml
as follows:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_check" android:state_checked="true" />
</selector>
To summarize:
- A custom check-mark in
drawableRight
does not render, regardless of whether the button is checked. - Using the standard
listChoiceIndicatorSingle
indrawableRight
does correctly respond to whether the button is selected - Using the custom check-mark in
button
does show the expected selection behavior of appearing and disappearing, but does not listen to spacing