Is there an easy way to add padding between the checkbox in a CheckBox control, and the associated text?
I cannot just add leading spaces, because my label is multi-line.
As-is, the text is way too close to the checkbox:
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.
Given @DougW response, what I do to manage version is simpler, I add to my checkbox view:
where the dimen is found in two values folders:
<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.