Skip to content

Instantly share code, notes, and snippets.

@creativedrewy
Last active January 25, 2024 07:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save creativedrewy/158bbab94f6beb61d5760e5507ee8655 to your computer and use it in GitHub Desktop.
Save creativedrewy/158bbab94f6beb61d5760e5507ee8655 to your computer and use it in GitHub Desktop.
## Quick Hit: styling an Android checkbox w/ AppCompat compatibility

The Problem

It turns out trying to find information about how to style an Android checkbox is actually quite challenging. Searching reveals a smattering of blog posts and Statck Overflow articles that don't really solve the problem. Here's what I was trying to do:

  • Change the default unchecked "open box" color on a checkbox
  • Change the default checked box color on a checkbox
  • Use in an API leve 18 Android app

It turns out the solution is relatively simple.

The Solution

First, create a new style in your styles.xml:

<style name="MyCheckboxStyle">
    <item name="colorControlNormal">@color/your_unchecked_color</item>
    <item name="colorAccent">@color/checked_box_color</item>
</style>

colorControlNormal is the color of the open-box/border when the checkbox is unchecked. colorAccent is the filled-in color when the checkbox is checked. Note that you do not prefix the "name" values with "android:" as is common for most style params. This is because that prefix is only for API level 21 and above. Omitting it maintains compatibility with older Android versions.

Next, to use the style on your checkbox, you (unintuitively) use the "theme" attribute in your checkbox xml:

<CheckBox
    android:theme="@style/MyCheckboxStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

And now your checkbox has your pretty custom colors!

@weeveedenny
Copy link

Splendid.

@Mohdtalib1
Copy link

Perfect solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment