Reputation: 3215
I'm using a check-box in my project, but I'm not able to eliminate the left padding, so the alignment isn't right.
Anyone know how to eliminate that left padding?
Upvotes: 2
Views: 1971
Reputation: 7974
This is because the drawable used by Android Checkbox has its own margin. So to avoid it you''ll have to create your own checkbox with a custom drawable and icons for checked and unchecked states.
1> Create a custom selector for your checkbox like:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@drawable/ic_unchecked" />
<item android:state_checked="true" android:drawable="@drawable/ic_checked" />
<item android:drawable="@drawable/ic_unchecked" /> <!-- default -->
</selector>
2>Then in your xml add a normal check box ,set drawable and button properties as null and assign your selector as the left drawable of your checkbox.Also give a drawable padding to give some padding between checkbox icon and text.
<CheckBox
android:id="@+id/chk_terms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:button="@null"
android:drawableLeft="@drawable/custom_checkbox"
android:drawablePadding="@dimen/margin_left_right"
android:text="@string/i_agree"
android:textSize="@dimen/text_size_small"
/>
NOTE ->
This soln also solve the padding problems of checkbox on and above android 4.2. which is also referred HERE
Upvotes: 1
Reputation: 3215
After some research I found out that, this specific margin is in the drawable of the check-square. So I suppose that the only solution is to create a custom drawable
Upvotes: 1