Bhavesh Jabuvani
Bhavesh Jabuvani

Reputation: 359

Checkbox is not checked

When I change a button of checkbox from xml, checkbox doesn't reflect checked. My xml file is:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

<TextView
    android:id="@+id/memberName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="57.56"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:textColor="#000000"/>

<CheckBox
    android:id="@+id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@drawable/checkbox"   <----- change button of checkbox
     />

@drawable/checkbox is png file

Upvotes: 0

Views: 656

Answers (1)

FoamyGuy
FoamyGuy

Reputation: 46856

In order to work android:button needs to be to be set to a selector xml file that defines drawables for both checked and unchecked states. Try setting it up like this:

checkbox_selector.xml

<?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/checkbox_checked" />
    <item android:state_checked="false" android:drawable="@drawable/checkbox_unchecked" />
</selector>

where checkbox_checked.png is the image you want to use when the box is checked. and checkbox_unchecked.png is the image when it is not checked.

Then in your main layout set the button like this:

<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/checkbox_selector"
 />

Upvotes: 1

Related Questions