2

I am developing an application in which i am using check box and applied selector on that. My code of check box is as follows :

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="0.5"
    android:button="@drawable/custom_checkbox"
    android:clickable="false"
    android:focusable="false"
    android:gravity="center" />

custom_checkbox.xml

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

    <item android:drawable="@drawable/checkbox_checked" android:state_checked="true"/>
    <item android:drawable="@drawable/checkbox_checked" android:state_pressed="true"/>
    <item android:drawable="@drawable/checkbox_unchecked"/>

</selector>

checkbox_checked

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

<item android:drawable="@drawable/green_tic"/>
    <item>
        <shape android:shape="rectangle" >
            <corners android:radius="@dimen/corner_radius_for_ask_option"/>
            <size
                android:height="20dp"
                android:width="20dp" />
        </shape>
    </item>

</layer-list>

checkbox_unchecked

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

    <item>
        <shape android:shape="rectangle" >
            <stroke
                android:width="2px"
                android:color="@color/color_light_grey" />

            <corners android:radius="@dimen/corner_radius_for_ask_option" />

            <size
                android:height="20dp"
                android:width="20dp" />
        </shape>
    </item>

</layer-list>

The above code is working fine on Note 3 and same kind of phone. It looks like :

uncheckchecked

But on samsung s2 it looks like :

uncheckchecked

Manoj Fegde
  • 4,786
  • 15
  • 50
  • 95
  • Looks like this is [common trouble](http://stackoverflow.com/questions/3965484/custom-checkbox-image-android). `android:button` and `android:drawable` seem to be the candidates. What happens if you change to `android:drawable=`? – dhke Jan 14 '16 at 10:51
  • actually checkbox's default color varies from device to device. And also try adding checked false attribute with your unchecked item line – Vivek Mishra Jan 14 '16 at 11:01
  • I had to use `app:useMaterialThemeColors="false"` – EpicPandaForce Nov 17 '22 at 09:37

1 Answers1

5

Try adding a transparent solid to your shapes:

<shape android:shape="rectangle" >
    <corners android:radius="@dimen/corner_radius_for_ask_option"/>
    <size
        android:height="20dp"
        android:width="20dp" />
    <solid android:color="@android:color/transparent"/>
</shape>

Same for the other drawable XML.

Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71