[FIXED] How to use selector with xamarin radiobutton to set background colors?

Issue

I have two radio buttons, which looks like a normal buttons. I would like to change their background color by using radio_button.xml with selector inside.
If I use little .png image to set background then it works fine.
But I tried similar code to use just colors (integers) and it doesn’t work. What I’m doing wrong?

Code with background image:

<RadioGroup
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <RadioButton
        android:text="LEFT"
        android:id="@+id/radioLeft"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:background="@drawable/radio_button"
        android:checked="true" />
    <RadioButton
        android:text="RIGHT"
        android:id="@+id/radioRight"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:background="@drawable/radio_button" />
</RadioGroup>

radio_button.xml

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_checked="true"
      android:drawable="@drawable/gray_dot" />
  <item android:state_checked="false"
      android:drawable="@drawable/blue_dot" />
</selector>

button_colors.xml

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <color name="button_black">#000000</color>
  <color name="button_white">#ffffff</color>
  <color name="button_blue">#00afdb</color>
  <color name="button_gray">#dcdcdc</color>
</resources>

I tried to replace

android:drawable="@drawable/blue_dot"

with

android:background="@color/button_blue"

but it’s not working. How to fix?

Solution

I used shape xml with color to avoid background images.

radio_button.xml

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_checked="true" 
      android:drawable="@drawable/rb_checked" />
  <item android:state_checked="false"
      android:drawable="@drawable/rb_unchecked" />
</selector>

rb_checked.xml

<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
       android:shape="rectangle" >
  <solid android:color="@color/button_blue" />
</shape>

Main.axml

<RadioGroup
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <RadioButton
        android:checked="true"
        android:text="LEFT"
        android:id="@+id/radioLeft"
        android:button="@android:color/transparent"
        android:background="@drawable/radio_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" />
    <RadioButton
        android:text="RIGHT"
        android:id="@+id/radioRight"
        android:button="@android:color/transparent"
        android:background="@drawable/radio_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" />
</RadioGroup>

Buttons in the cell phone emulator:

Answered By – Deleted because of negativity

Answer Checked By – Dawn Plyler (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published