Manoj Fegde
Manoj Fegde

Reputation: 4771

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

Upvotes: 2

Views: 3128

Answers (2)

PkAndroid dev
PkAndroid dev

Reputation: 1

to solve this you should use app:button="@drawable/custom_checkbox" instead of android:button="@drawable/custom_checkbox"

Upvotes: 0

Daniel Zolnai
Daniel Zolnai

Reputation: 16920

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.

Upvotes: 5

Related Questions