androidandroid-layoutcheckboxandroid-checkboxandroid-selector

Android: Check box selector not working properly


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


Solution

  • 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.