Change icons of checked and unchecked for Checkbox for Android

JavaAndroidCheckboxIcons

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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>

Solution 2 - Java

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

Solution 3 - Java

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.

Solution 4 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMohit DeshpandeView Question on Stackoverflow
Solution 1 - JavaggomezeView Answer on Stackoverflow
Solution 2 - Javaалекс кейView Answer on Stackoverflow
Solution 3 - JavagiovaniguerraView Answer on Stackoverflow
Solution 4 - JavaRavi VyasView Answer on Stackoverflow