0

I have

Checkbox.setButtonDrawable(R.drawable.ic_checkbox_disabled);

Here i am setting to custom image


How to set to default check box background

I tried:

Checkbox.setButtonDrawable(null);

But entire box diseappered So I need the resource name of the checkbox android uses


Something like:

Checkbox.setButtonDrawable(R.android.resourcename);

Any solution for this

Devrath
  • 42,072
  • 54
  • 195
  • 297
  • Possible duplicate of [How to change default images of CheckBox](http://stackoverflow.com/questions/7783787/how-to-change-default-images-of-checkbox) – Hitesh Sahu Feb 01 '17 at 08:02
  • Try {Checkbox.setButtonDrawable(android.R.drawable.checkbox_off_background)} – Zahid Feb 01 '17 at 08:04

2 Answers2

4

Create a new selector file in drawable folder with name checkbox_selection :

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:drawable="@drawable/checked" 
              android:state_checked="false"/>
        <item android:drawable="@drawable/uncheck" 
              android:state_checked="true"/>
        <item android:drawable="@drawable/checkbox"/>    
    </selector>

Modify your checkbox view in xml file as show below :

<CheckBox
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:button="@drawable/checkbox_selection"
     android:text="CheckBox"
     android:textColor="@color/Black" >
</CheckBox>
Sid Mhatre
  • 3,272
  • 1
  • 19
  • 38
Sushil Dubey
  • 686
  • 1
  • 5
  • 20
  • When will `checkbox` drawable be used? The `state_checked="true"` already covers all the rest cases, no? Also, I think the drawable names are the opposite of what you intended (`checked` is for when it's unchecked, and vice versa) – android developer Dec 05 '17 at 14:54
1

In drawable folder, make one file and name anything you want. Then paste this.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/radio_checked_icon" android:state_checked="true"/>
    <item android:drawable="@drawable/radio_unchecked_icon" android:state_checked="false"/>


</selector>

radio_checked_icon and radio_unchecked_icon, these are the custom images you want to put, when your checkbox is clicked, then radio_checked_icon, else radio_unchecked_icon

and then in layout use like this:

 <CheckBox
            android:id="@+id/tiny_radio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:button="@drawable/name_of_your_file_present_in_Drawable"
            android:padding="@dimen/padding_short" />
Aman Shekhar
  • 2,719
  • 1
  • 18
  • 29