I'm trying to apply custom image to checkbox in android, for this I create an check_custom.xml
file in which I define custom image for different states of check box like:
<item android:state_checked="true" android:drawable="@drawable/btn_check_on" /> <!-- checked --> <item android:state_checked="false" android:drawable="@drawable/btn_check_off" /> <!-- unchecked --> <item android:state_focused="true" android:drawable="@drawable/btn_check_onfocus" /> <!--on focus--> <item android:drawable="@drawable/btn_check_off" /> <!-- default -->
Three different images on three states on checked,on focus and on unchecked, and then I assign this xml file to background attribute of check boxes,but I'm not getting required result, this technique apply the custom image as well as default image both.
I had to set android:drawable="@drawable/checkbox_selector" instead and also set android:button="@null" . You can also add android:drawablePadding to make it look good. However, this makes the entire checkbox clickable (along with the text).
To define the click event handler for a checkbox, add the android:onClick attribute to the <CheckBox> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event.
This example demonstrates how do I change the color of the check box in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.
Salman, set android:button
instead of android:background
. See also Custom checkbox image android
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With