1

I have a very simple custom checkbox selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_checked="true" android:drawable="@drawable/ui_checkbox_checked" />
   <item android:drawable="@drawable/ui_checkbox" />
</selector>

that I'm setting using the following code:

<CheckBox android:id="@+id/checkbox1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center_horizontal"
   android:button="@drawable/checkbox_selector"
   android:textSize="12dp"
   android:text="checkbox 1"
   />

The custom checkbox image is smaller than the standard image that comes with Android, but the text is still in exactly the same spot, as if the larger image were still being used.

So instead of looking like [X]_checkbox 1 it looks like [X]____checkbox 1 with a big space between the box and the text. How can I fix this?

Micah Hainline
  • 14,367
  • 9
  • 52
  • 85

3 Answers3

4

One potential solution is to set the checkBox to have no text. Then make yourself a TextView and use android:layout_toRightOf="@+id/checkbox1"

Probably is non-ideal since it means using an additional View, but this is how I've solved the problem that you currently face in the past.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • I've done this in the past as well. :( It also requires keeping track of touches on the TextView and making this check the box as well. It's a real pain! – Micah Hainline Mar 12 '12 at 14:19
  • true, looks like you can do it by hardcoding the padding in java (from @Abu second link) Still probably non-ideal. I might make a custom view class to handle it if I were you =(. Its too bad they didn't add an extra "spacer" value or something. – FoamyGuy Mar 12 '12 at 14:24
  • @FoamyGuy good suggestion, but i think a `TextView` can be turned into a `CheckBox` by using `drawableLeft` :) – Muhammad Babar Mar 18 '14 at 06:41
1

Size of control depends on foreground and background sizes. When you customize checkbox - you make custom foreground, but background is left from android. If your custom foreground is larger then android's background there is no problem. If you use smaller custom foregraund then size of checkbox is calculated from size of system background (which isn't good). Try to use small transparent .png as background: android:background="@drawable/small_bg" (or make experiments with some .9.png)

Sergey Burish
  • 584
  • 6
  • 8
0

You can set android:paddingLeft attribute to avoid extra space between image and text view Example

 <CheckBox android:id="@+id/checkbox1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="center_horizontal"

   android:textSize="12dp"
   android:text="checkbox 1"
   android:paddingLeft="0dip"
   />
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
  • 0 is not a good value here. It would have to be the button width + text spacing – Micah Hainline Mar 12 '12 at 14:40
  • @Micah, Here I have put 0 only for a reference. There is some situations when you need to use 0. You need to calcuclate by yourself what margin do you want. – Sunil Kumar Sahoo Mar 12 '12 at 14:45
  • What I meant by that was that the paddingLeft sets the padding for the left side of the text with respect to the left side of the entire checkbox control. Thus 0 will make the button overlap the text. The button width is the minimum value you want to put here. – Micah Hainline Mar 12 '12 at 14:57