I’m trying to style the CheckBox-elements I’m using in my layout.
Since I want the style consistent across different Activities, I’ve created a custom Theme that I apply in the manifest.
The styles.xml file looks like this:
<style name="LightTheme" parent="@android:style/Theme.NoTitleBar">
.
.
<item name="android:checkboxStyle">@style/CustomCheckBox</item>
.
.
</style>
.
.
<style name="CustomCheckBox" parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:button">@drawable/btn_check_selector</item>
<item name="android:textColor">@color/mainTextColorLight</item>
<item name="android:textSize">15dp</item>
</style>
.
.
The selector, which is basically identical to the CheckBox selector found in the SDK, looks like this:
<!-- Enabled states -->
<item android:state_checked="true" android:state_window_focused="false"
android:state_enabled="true"
android:drawable="@drawable/btn_check_on" />
<item android:state_checked="false" android:state_window_focused="false"
android:state_enabled="true"
android:drawable="@drawable/btn_check_off" />
<item android:state_checked="true" android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/btn_check_on_pressed" />
<item android:state_checked="false" android:state_pressed="true"
android:state_enabled="true"
android:drawable="@drawable/btn_check_off_pressed" />
<item android:state_checked="true" android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/btn_check_on_focused" />
<item android:state_checked="false" android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/btn_check_off_focused" />
<item android:state_checked="false"
android:state_enabled="true"
android:drawable="@drawable/btn_check_off" />
<item android:state_checked="true"
android:state_enabled="true"
android:drawable="@drawable/btn_check_on" />
<!-- Disabled states -->
<item android:state_checked="true" android:state_window_focused="false"
android:drawable="@drawable/btn_check_on_disabled" />
<item android:state_checked="false" android:state_window_focused="false"
android:drawable="@drawable/btn_check_off_disabled" />
<item android:state_checked="true" android:state_focused="true"
android:drawable="@drawable/btn_check_on_disabled_focused" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="@drawable/btn_check_off_disabled_focused" />
<item android:state_checked="false" android:drawable="@drawable/btn_check_off_disabled" />
<item android:state_checked="true" android:drawable="@drawable/btn_check_on_disabled" />
The selector is stored in the ‘drawable’ folder of my project, and the density specific resources are stored in the drawable-{l|m|h|xh}dpi folders.
The weird thing is that this worked very well for a long time. But today, its suddenly not working anymore. The CheckBox-elements are now showing up without the box-part. Only the text is visible.
All the other custom styled elements works as intended, while the CheckBox displays this strange behavior.
I’ve tried to apply the style directly to a CheckBox with xml, with no luck. Also done the usual ‘Clean Project’. The only way I can get this to work, is to copy the selector to all the drawable-{l|m|h|xh}dpi folders in my project. I was under the impression that it was not necessary to have a copy of the selector in every drawable folder, and it certainly is not necessary for my other custom selectors.
Can anyone spot any obvious flaws in my code?
Finally solved it, and the problem seemed to be some sort of name conflict.
Renamed my selector from ‘btn_check_selector.xml’ to ‘selector_check.xml’, and did a corresponding change in my CustomCheckBox style:
The selector is in the ‘drawable’ folder of my project, and the density specific resources are in their respective drawable-{l|m|h|xh}dpi folders.
This is a bit strange, as I do not have any other files named ‘btn_check_selector’ anywhere in my project. My only guess is that Eclipse was acting up!