Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change default images of CheckBox

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 ?

like image 556
Damir Avatar asked Oct 16 '11 10:10

Damir


2 Answers

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" /> 
like image 162
A.Quiroga Avatar answered Sep 20 '22 23:09

A.Quiroga


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" /> 
like image 39
Blackbelt Avatar answered Sep 22 '22 23:09

Blackbelt