I’ve got a checkbox with a state list drawable and my own graphics (it’s for a game). The built-in checkbox graphics are apparently 72×72, and my graphics are 72 high, but only 27 wide.
Snippet of my main.xml layout:
<CheckBox android:id="@+id/r_diff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/reset"
android:layout_alignParentBottom="true"
android:onClick="onClickRDiff"
android:button="@drawable/console_select_switch" />
And here’s my console_select_switch.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector android:constantSize="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/consw_cntr" /> <!-- pressed -->
<item android:state_checked="true"
android:drawable="@drawable/consw_down" /> <!-- checked -->
<item android:drawable="@drawable/consw_up" /> <!-- default -->
</selector>
Now, when I run the program, my graphics do show up, and they do work, BUT they take up as much width as the original square 72 pixel graphic. I’ve even tried setting
android:width="27px"
in the selector item statements, also without effect. I even tried turning constantSize to ‘false’, but that had no effect.
I’m unclear what the heck I’m doing wrong – my understanding is that wrap_content should cause it to be ‘as wide as it needs to be’, so I assumed that it would just work. Interestingly, when I used some of the same graphics as android:background in a regular button, it works fine.
Edit: the API level on my project is set to 7 (Android 2.1), if that has any relevance (looking at the docs, I think all of what I’m using has been in place for a long time, so I doubt it).
Edit: I just played with setting the text on this control (I tried various values including an empty string for the android:text parameter) and the text is drawing to the right of the area that the checkbox is consuming (not the area actually occupied by the graphic). So clearly something thinks that this checkbox is 72 pixels wide, when it’s really 27.
OK, I finally figured it out. This link leads to a detailed look at changing the appearance of the checkbox, wherein I figured out that the default background is in fact an image (it’s a 9-patch, to accommodate the text). The underlying background image turns out to e 60×60, and was thus the cause of my troubles. I’ve put a background image of my own in my resources and referenced it from the layout by adding
to the checkbox definition in the layout.