Android - Spacing between CheckBox and text
Solution 1:
I hate to answer my own question, but in this case I think I need to. After checking it out, @Falmarri was on the right track with his answer. The problem is that Android's CheckBox control already uses the android:paddingLeft property to get the text where it is.
The red line shows the paddingLeft offset value of the entire CheckBox
If I just override that padding in my XML layout, it messes up the layout. Here's what setting paddingLeft="0" does:
Turns out you can't fix this in XML. You have do it in code. Here's my snippet with a hardcoded padding increase of 10dp.
final float scale = this.getResources().getDisplayMetrics().density;
checkBox.setPadding(checkBox.getPaddingLeft() + (int)(10.0f * scale + 0.5f),
checkBox.getPaddingTop(),
checkBox.getPaddingRight(),
checkBox.getPaddingBottom());
This gives you the following, where the green line is the increase in padding. This is safer than hardcoding a value, since different devices could use different drawables for the checkbox.
UPDATE - As people have recently mentioned in answers below, this behavior has apparently changed in Jelly Bean (4.2). Your app will need to check which version its running on, and use the appropriate method.
For 4.3+ it is simply setting padding_left. See htafoya's answer for details.
Solution 2:
Given @DougW response, what I do to manage version is simpler, I add to my checkbox view:
android:paddingLeft="@dimen/padding_checkbox"
where the dimen is found in two values folders:
values
<resources>
<dimen name="padding_checkbox">0dp</dimen>
</resources>
values-v17 (4.2 JellyBean)
<resources>
<dimen name="padding_checkbox">10dp</dimen>
</resources>
I have a custom check, use the dps to your best choice.
Solution 3:
Use attribute android:drawableLeft
instead of android:button
. In order to set padding between drawable and text use android:drawablePadding
. To position drawable use android:paddingLeft
.
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:drawableLeft="@drawable/check_selector"
android:drawablePadding="-50dp"
android:paddingLeft="40dp"
/>
Solution 4:
Android 4.2 Jelly Bean (API 17) puts the text paddingLeft from the buttonDrawable (ints right edge). It also works for RTL mode.
Before 4.2 paddingLeft was ignoring the buttonDrawable - it was taken from the left edge of the CompoundButton view.
You can solve it via XML - set paddingLeft to buttonDrawable.width + requiredSpace on older androids. Set it to requiredSpace only on API 17 up. For example use dimension resources and override in values-v17 resource folder.
The change was introduced via android.widget.CompoundButton.getCompoundPaddingLeft();