Home » Android » android – Rectangle shape drawable won't show as checkbox selector item

android – Rectangle shape drawable won't show as checkbox selector item

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have defined a rectangle drawable like this:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle">

    <corners android:radius="4dip"/>
    <solid android:color="#FF000000" />
    <stroke android:width="1dip" android:color="@color/service_checkbox_disabled_unchecked_stroke" />

</shape>

I can display the drawble as imageView without problems. However, it is supposed to be a drawble for one state of a checkbox. My selector for the checkbox button is defined like this:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >

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

</selector>

And finally my checkbox:

<CheckBox
    android:id="@+id/cb_tariff_3_next_month_checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@drawable/checkbox_services"
    android:layout_centerHorizontal="true"/>

Can anyone tell, why does that not work? Thank you very much.

How to&Answers:

You must add a size node to the shape:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
    >
    <size android:width="30dp" android:height="30dp" />
    <solid android:color="@color/blueBase"/>
    <stroke
        android:width="1dp"
        android:color="@color/blueDark" />
</shape>