RobVoisey
RobVoisey

Reputation: 1083

RadioButton 'android:button' does nothing

I've tried to change the drawable of the checked button on a RadioButton but nothing is showing; setting the drawable just removes the default one that is already there.

I used stack overflow to try find a solution but when I try to implement it, there is not drawable showing up. My selector appears to be fine because when I set the background of the radio button that displays my circles.

I'm sure it's a very obvious problem which I'll slap myself for not seeing; can anyone figure out what is wrong?

My Radio Buttons

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Left"
            android:checked="false"
            android:button="@drawable/radio_button" />
        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/radio_button"
            android:text="Right"
            android:checked="true" />
    </RadioGroup>

With my selector

with selector

Without my selector

without selector

My Selector XML

<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@drawable/radio_button_checked"
        android:state_checked="true" />
    <item
        android:drawable="@drawable/radio_button_unchecked" />
</selector>

radio_button_checked.xml

<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="@color/primary_button_color"/>
    <stroke
        android:width="2px"
        android:color="#000000"/>
</shape>

radio_button_unchecked.xml

<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <stroke
        android:width="2px"
        android:color="#000000"/>
</shape>

Upvotes: 1

Views: 63

Answers (2)

CoXier
CoXier

Reputation: 2663

Your Problem

As you see, RadioButton extends CompoundButton. When you do not use your selector, there is a default drawable.Let's see its constructor.

   public CompoundButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);

    final TypedArray a = context.obtainStyledAttributes(
            attrs, com.android.internal.R.styleable.CompoundButton, defStyleAttr, defStyleRes);

    final Drawable d = a.getDrawable(com.android.internal.R.styleable.CompoundButton_button);
    if (d != null) {
        setButtonDrawable(d);
    }

The default drawable is R.styleable.CompoundButton_button whose intrinsicWidth = 96px and intrinsicHeight = 96px on my device.

When you use your selector, the drawable's intrinsicWidth = -1px and intrinsicHeight = -1px so you can not see it.

Solution

You should define your drawable's size.

 <?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="oval">
<size android:width="24dp" android:height="24dp"/>
<stroke
    android:width="2px"
    android:color="#000000"/>
</shape>

Upvotes: 1

Eselfar
Eselfar

Reputation: 3869

I think you can't see anything because your drawable don't have any size.

Try to define <size android:width="60dp" android:height="60dp"/> under the shape of your xml.

Upvotes: 2

Related Questions