Android - Spacing between CheckBox and text
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.
Android - Spacing between CheckBox and text
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.
Why do i get a gap between checkbox and text?
Try messing with the android:paddingLeft property of the checkbox.
See this post for further information: Android - Spacing between CheckBox and text
Left and top padding for CheckBox doesn't work
You have to use android:layout_marginLeft
for this usecase.
Padding is the space inside the component. e.g: space between the text and the border of TextView.
Margin is the space outside the components. e.g: space between left edge of the screen and border of your component
spacing for checkmark image of the check box in android
this is resolved with below code.
<CheckBox
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/filter_item_background"
android:button="@android:color/transparent" //Make button null
android:drawableLeft="@drawable/btn_check_square" //Create button in drawable
android:drawablePadding="20dp" //Set space between Text & CB
android:padding="5dp"
android:text="check box text"
android:textColor="@android:color/black" />
How to remove padding around Android CheckBox
You could use a negative margin.
android:layout_marginTop = "-5dp"
android:layout_marginRight = "-5dp"
How do I fix the checkbox text spacing on pre-lollipop devices?
Just to close the thread, I am posting the answer here:
The solution is to use parent="Widget.AppCompat.CompoundButton.CheckBox"
to support lower APIs as well.
Related Topics
Textview with Different Textsize
Android:Configchanges="Orientation" Does Not Work with Fragments
Cardview Layout_Width="Match_Parent" Does Not Match Parent Recyclerview Width
How to Style the Standard React-Native Android Picker
How to Display Image in Android's Textview
Programmatically Obtain the Android API Level of a Device
How to Take Camera Capture Without a Preview from a Service or Thread
How to Send Broadcast from One App to Another App
Android Material: Status Bar Color Won't Change
Incompatible Plugins for Android-Apt After Upgrading to Android Studio 2.3
Android Speech Recognition Continuous Service
Pathpattern to Match File Extension Does Not Work If a Period Exists Elsewhere in the File Name
How to Set Android_Sdk_Home Environment Variable
Detect Touch Event on a View When Dragged Over from Other View
Get Cursor Position in Android in Edit Text
How to Start an Intent by Passing Some Parameters to It
Android, How to Not Destroy the Activity When I Rotate the Device