Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
## 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!

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