12

I have tried to make a xml selector with the following:

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

and when I try to set the backgroundDrawable to the checkBox the checkbox doesn't replace the CheckBox style too:

  shuffle.setBackground(android.support.v4.content.res.ResourcesCompat.getDrawable(getResources(), R.drawable.shuffle, null));

Following this question: Change icons of checked and unchecked for Checkbox for Android I need to set the button drawable with my xml: android:button="@drawable/checkbox" but I can't do this because I'm creating the CheckBox programmatically. Is there a way how to achieve this?

Community
  • 1
  • 1
Marian Pavel
  • 2,726
  • 8
  • 29
  • 65

6 Answers6

24

Simple way

  1. Create a new layout in drawable folder and name it custom_checkbox (You can rename also)

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/Checked_image_name"android:state_checked="true"/>
    <item android:drawable="@drawable/Unchecked_image_name" android:state_checked="false"/>
    </selector>
    
  2. Use this in your layout activity

    <CheckBox
    android:id="@+id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@drawable/custom_checkbox"/>
    
Community
  • 1
  • 1
Sunil
  • 3,785
  • 1
  • 32
  • 43
12

Use below line of code, i think it will work

yourcheckbox.setButtonDrawable(yourdrawable); 
Madhu
  • 1,780
  • 23
  • 47
5

Use This

shuffle.setButtonDrawable(R.drawable.custom_checkbox_selector);

XML code;

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

    <item android:drawable="@drawable/radiobutton_checked" android:state_checked="true"/>
    <item android:drawable="@drawable/radiobutton_unchecked" android:state_checked="false"/>

</selector>
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
koutuk
  • 832
  • 1
  • 8
  • 17
5

If you use sdk 28 (androidx) and above try to use

androidx.appcompat.widget.AppCompatCheckBox

instead of

Checkbox
Mikhail Valuyskiy
  • 1,238
  • 2
  • 16
  • 31
4

This works for me:

<android.support.v7.widget.AppCompatCheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/custom_checkbox"
android:button="@null"/>
TetianaDev
  • 373
  • 2
  • 8
1

drawable/checkbox_custome.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/checkbox_empty" />
    <item android:state_checked="true"
        android:drawable="@drawable/checkbox_check" />
</selector>
Tienanhvn
  • 239
  • 5
  • 9