I am using ListView and for every row I have row_item.xml and I inflate that in code
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <CheckBox android:id="@+id/chk" android:layout_alignParentLeft="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/txtChoice" android:textColor="#FF0000" android:text="TEST" android:layout_toRightOf="@id/chk" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </RelativeLayout>
How to change that checkBox use another custom_1 image when is checked and another custom_2 image when is unchecked ?
Drawable customdrawablecheckbox.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/unchecked_drawable" /> <item android:state_checked="true" android:drawable="@drawable/checked_drawable" /> <item android:drawable="@drawable/unchecked_drawable" /> <!-- default state --> </selector>
yourcheckbox xml:
<CheckBox android:id="@+id/chk" android:button="@drawable/customdrawablecheckbox" android:layout_alignParentLeft="true" android:layout_width="wrap_content" android:layout_height="wrap_content" />
checkbox is a button, so you can provide your own drawable with check uncheck state and it as checkbox background. For instance
<?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/yourdrawable1" /> <item android:state_checked="true" android:drawable="@drawable/yourdrawable2" /> <item android:drawable="@drawable/yourdrawable1" /> <!-- default --> </selector>
and put this in a file.xml in your drawable folder. In your checkbox:
<CheckBox android:button="@drawable/file" android:id="@+id/chk" android:layout_alignParentLeft="true" android:layout_width="wrap_content" android:layout_height="wrap_content" />
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