Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply custom image to checkbox in android

Tags:

android

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.

like image 287
salman roy Avatar asked Mar 03 '11 05:03

salman roy


People also ask

How to make custom checkbox in android Studio?

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

Which XML element is used to design a custom checkbox in android?

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.

How can I change checkbox background in android?

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.


1 Answers

Salman, set android:button instead of android:background. See also Custom checkbox image android

like image 162
DJC Avatar answered Sep 28 '22 08:09

DJC