KousiK
KousiK

Reputation: 825

Android CheckBox Custom image not showing properly

In my project I have a CheckBox and I am trying to add a Custom image for checked and unchecked state but the images are not showing properly for xhdpi screen.Kindly help

I am posting the xml and screen shots

custom_checkbox.xml

 <?xml version="1.0" encoding="utf-8"?>
       <selector  xmlns:android="http://schemas.android.com/apk/res/android">
       <item android:state_checked="false" android:drawable="@drawable/ic_uncheck" />
       <item android:state_checked="true" android:drawable="@drawable/ic_check" />
       <item android:drawable="@drawable/ic_uncheck" /> <!-- default -->
 </selector>

check_box.xml

     <CheckBox
        android:id="@+id/chkVibrate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:button="@drawable/checkbox"
        android:text="CheckBox Text"
        android:textColor="@color/white" />

click here for the hdpi image

click here for the xhdpi image

Upvotes: 2

Views: 2309

Answers (1)

Vaibhav Agarwal
Vaibhav Agarwal

Reputation: 4499

You should use your custom xml file for button property

 <CheckBox
    android:id="@+id/chkVibrate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:button="@drawable/custom_checkbox"
    android:text="CheckBox Text"
    android:textColor="@color/white" />

you can create a custom file in this way too if you want to set different image in different focused states.

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_checked="true" 
    android:drawable="@drawable/ic_check"
    android:state_focused="false">
</item>
<item android:state_checked="true" 
    android:drawable="@drawable/ic_check"
    android:state_focused="true">
</item>
<item android:state_checked="false" 
    android:drawable="@drawable/ic_uncheck"
    android:state_focused="false">
</item>
<item android:state_checked="false" 
    android:drawable="@drawable/ic_uncheck"
    android:state_focused="true">
</item>
</selector>

Upvotes: 1

Related Questions