-1

Im trying to make custom Radio buttons. Found an article which looked great (Article) but when i try doing what is said here, it doesnt update at all. Would a base theme prevent me from making custom radio buttons? My code below:

Layout xml

    <RadioGroup
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:id="@+id/radioGroup"
    android:background="@drawable/country_select_radio_button">

    <RadioButton
        android:id="@+id/ke_radio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/SouthAfrica"
        android:layout_alignStart="@+id/SouthAfrica"
        android:layout_alignTop="@+id/SouthAfrica"
        android:layout_marginTop="35dp"
        android:background="@drawable/country_select_radio_button"
        android:text="Kenya"
        android:layout_gravity="center_horizontal"
        android:checked="false" />

    <RadioButton
        android:id="@+id/za_radio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_za"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="125dp"
        android:text="South Africa"
        android:checked="true"
        android:layout_gravity="center_horizontal" />

</RadioGroup>

button style xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:drawable="@drawable/b"
    android:state_checked="true"
    android:state_pressed="true" />
<item
    android:drawable="@drawable/a"
    android:state_pressed="true" />
<item
    android:drawable="@drawable/a"
    android:state_checked="true" />
<item
    android:drawable="@drawable/b" />

a.xml

<selector
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
    android:radius="5dp" />
<solid
    android:color="#fff" />
<stroke
    android:width="2dp"
    android:color="#FF0000" />

b.xml

<selector
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
    android:radius="5dp" />
<solid
    android:color="#FF0000" />
<stroke
    android:width="2dp"
    android:color="#555555" />

no matter what i change, the style of the button stays the default?

what am i doing wrong?

Community
  • 1
  • 1
Stillie
  • 2,647
  • 6
  • 28
  • 50

1 Answers1

1

u need to set the android button property in your xml

                <RadioButton
                    android:id="@+id/ke_radio"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:button="@drawable/radio_button_selector"
                    android:padding="@dimen/text_padding"
                    android:paddingLeft="10dp"
                  />

wher radio_button selector is your custom xml file for radio button

please use below selector xml file

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/radiobutton1" android:state_checked="false"/>
<item android:drawable="@drawable/radiobutton2" android:state_checked="true"/>
<item android:drawable="@drawable/radiobutton1"/>

</selector>

put 2 images for testing as radiobutton1 and radiobutton2 and test

Arun Antoney
  • 4,292
  • 2
  • 20
  • 26
  • when i change android:background to android:button it totally removes all formatting and is just text? when its on android:background it atleast looks like a radio button. – Stillie Apr 30 '15 at 08:40
  • See inorder to customize radio button you need use the android button property .You can't use background property to control radio button status.it will just perform as a background image nothing more.The error may be due to your custom style file – Arun Antoney Apr 30 '15 at 08:44