Search code examples
androidradio-buttonselectordrawable

There is no change in style when click on or off on Radiobutton


I want to make style RadioButton when clicking off or on using @drawable/radio_selected like the 2 images below.

Style RadioButton when clicking is off

Style RadioButton when clicking is on

<RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:background="@drawable/radio_selected"
        tools:ignore="UnusedAttribute" />

In this drawable, I use the selector to make a condition when the radio button is on or off.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/rb_on" android:state_checked="true"/>
    <item android:drawable="@drawable/rb_off" android:state_checked="false"/>
</selector>

rb_on.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="5dp"
    android:height="5dp"
    android:viewportWidth="19"
    android:viewportHeight="19">
  <path
      android:pathData="M9.5,9.5m-6.5,0a6.5,6.5 0,1 1,13 0a6.5,6.5 0,1 1,-13 0"
      android:strokeWidth="6"
      android:fillColor="#ffffff"
      android:strokeColor="#FF8830"/>
</vector>

rb_off.xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="5dp"
    android:height="5dp"
    android:viewportWidth="19"
    android:viewportHeight="19">
  <path
      android:pathData="M9.5,9.5m-8.5,0a8.5,8.5 0,1 1,17 0a8.5,8.5 0,1 1,-17 0"
      android:strokeWidth="2"
      android:fillColor="#ffffff"
      android:strokeColor="#FF8830"/>
</vector>

But, That doesn't work. The result so like this.

Radiobutton style results when off

Radiobutton style results when on

I have added several items such as "android:state_pressed", etc but the results are the same. How to solve that?


Solution

  • If you want to change the icon of RadioButton then you need to use android:button

    Use this

    android:button="@drawable/radio_selected"
    

    Instead of this

     android:background="@drawable/radio_selected"
    

    SAMPLE CODE

    <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="100dp"
            android:button="@drawable/radio_selected"
            tools:ignore="UnusedAttribute" />