For an App I’m writing I need to use custom checkboxes in a PreferenceScreen. I want to use the CheckboxPreferences as it’s designed for that. Many searches lead to StackOverflow, but caused me a stack overflow as well as they didn’t work. In this (short) post I outline my findings and provide a working solution step by step.
You need a custom checkbox. This can be defined in a drawable. In this case I’ve called the file checkbox.xml and placed it in the drawable folder. The code defines the images for two states: if the checkbox is enabled and when it’s disabled. For this file looks like this:
We need a layout for customized preferences. This layout defines the same stuff as the ‘ regular’ preference does (text, summary etc.). Note the last part in the following file (called checkbox_preference.xml and placed in the layout folder). It loads our custom checkbox!
Now the code below is the actual layout which you inflate with your SharedPreferences in your Activity. The layout attribute is used to put the custom layout with the custom checkbox as an item in there.
There is no step 4. You’re done! It shouldn’t be that hard to find and use these things. Unfortunately many same questions, but more different (and often not / not completely working replies / solutions) are posed on sites like StackOverflow which makes it harder to find the real solution. Hopefully this post is a worthy addition to the interwebs and a valuable resource for Android designers facing the same problem.