1

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?

2 Answers2

1

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" />
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • Got it. It's work. But, I want to know. I'm read from (https://stackoverflow.com/questions/19163628/adding-custom-radio-buttons-in-android) using "android: background" to set drawable selector radio button. So, when to use "android: background" or "android: button" in certain radio button cases? And what is different? – Kelvin Herwanda Tandrio Mar 18 '20 at 04:35
  • 1
    @KelvinHerwandaTandrio the post you have shared in that question the user want change the background of radio button not the icon, if you want to change background of `RadioButton` at that you need to use `android:background` and if you want to change the icon of `RadioButton` then you need to use `android:button` – AskNilesh Mar 18 '20 at 04:39
0

Use button instead of background in xml

android:background="@drawable/radio_selected" 

with

android:button="@drawable/radio_selected"
Ravi
  • 2,277
  • 3
  • 22
  • 37