2

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.

enter image description here

Anyone know how to eliminate that left padding?

Stefano
  • 3,127
  • 2
  • 27
  • 33
  • 1
    try using a negative left margin such as -10 dp – Anirudh Sharma May 07 '15 at 07:05
  • 1
    But this is not the right and elegant way to solve it :) Also because it could change from device to device – Stefano May 07 '15 at 07:14
  • 1
    if you are giving margin to your edittext and other views then set that margin as negative to the checkbox.It won't change then.Meanwhile i'll provide you with a better soln – Anirudh Sharma May 07 '15 at 07:17
  • 1
    a better soln is that you can set your checkbox button and backgound as null and then add a drawable left. eg- android:background="@null" android:button="@null" android:drawableLeft="@drawable/custom_checkbox" – Anirudh Sharma May 07 '15 at 07:20
  • 1
    Could be an idea, but I've to draw my own checkbox, I think that the problem is the drawable of the square, have margin itself. – Stefano May 07 '15 at 07:30
  • exaclty, i found a bug reported at google forums and somebody replied that it's because of the margins given to the drawable image in Checkbox.I Guess you''ll have to create your own custom checkbox drawable. – Anirudh Sharma May 07 '15 at 07:32
  • let me suggest by giving an example so that it may benifit others in future. – Anirudh Sharma May 07 '15 at 07:34

2 Answers2

1

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

Stefano
  • 3,127
  • 2
  • 27
  • 33
1

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

Community
  • 1
  • 1
Anirudh Sharma
  • 7,968
  • 13
  • 40
  • 42