71

Instead of having a check mark for the icon, I want a custom star (I have checked and unchecked icons). Can this be done through a property? Or must I declare a custom widget that derives from Checkbox?

Mohit Deshpande
  • 53,877
  • 76
  • 193
  • 251

4 Answers4

188

Kind of a mix:

Set it in your layout file :-

 <CheckBox android:layout_width="wrap_content"
           android:layout_height="wrap_content" 
           android:text="new checkbox"
           android:background="@drawable/checkbox_background" 
           android:button="@drawable/checkbox" />

where the @drawable/checkbox will look like:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_checked="true" android:state_focused="true"
  android:drawable="@drawable/checkbox_on_background_focus_yellow" />
 <item android:state_checked="false" android:state_focused="true"
  android:drawable="@drawable/checkbox_off_background_focus_yellow" />
 <item android:state_checked="false"
  android:drawable="@drawable/checkbox_off_background" />
 <item android:state_checked="true"
  android:drawable="@drawable/checkbox_on_background" />
</selector>
Felix D.
  • 4,811
  • 8
  • 38
  • 72
ggomeze
  • 5,711
  • 6
  • 29
  • 32
  • 1
    Same as with any other checkbox (http://developer.android.com/reference/android/widget/CheckBox.html). You can make use of of this method if manually want to set it to check or unchecked: http://developer.android.com/reference/android/widget/CompoundButton.html#setChecked(boolean) – ggomeze Feb 20 '13 at 09:41
  • @ggomeze what if we want to change the size of text as well ?? – Shaishav Jogani Jul 15 '16 at 12:01
  • Have you tried scale within the Checkbox?: android:scaleX="1.5" and android:scaleY="1.5" – ggomeze Jul 18 '16 at 11:05
  • 2
    `android:button` property doesn't work on pre-Lollipop devices – soshial Mar 24 '17 at 14:59
  • @soshial what should be the preferred compat method then? – Antek May 31 '17 at 09:51
13

it's android:button="@drawable/selector_checkbox" to make it work

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
13

This may be achieved by using AppCompatCheckBox. You can use app:buttonCompat="@drawable/selector_drawable" to change the selector.

It's working with PNGs, but I didn't find a way for it to work with Vector Drawables.

Ziad Akiki
  • 2,601
  • 2
  • 26
  • 41
giovaniguerra
  • 131
  • 1
  • 2
2

One alternative would be to use a drawable/textview instead of a checkbox and manipulate it accordingly. I have used this method to have my own checked and unchecked images for a task application.

Ravi Vyas
  • 12,212
  • 6
  • 31
  • 47