2

I have made a custom checkbox where checkbox text would be in left side and checked mark in the right side. My custom checkbox works perfectly but facing on problem. That is, my checkbox text is not aligning exactly to left. There is some space in the left. My code is.

<CheckBox
    android:id="@+id/chk_1" 
    android:text="DHKXXX" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:button="@null" 
    android:drawableRight="@xml/custom_checkbox" 
/>  
marco
  • 157
  • 1
  • 1
  • 9

5 Answers5

3

I think your default drawable is occupying the extra area. You can fix this by adding the following line.

android:layout_marginLeft="-40dp"
Imdad Sarkar
  • 1,245
  • 2
  • 12
  • 24
  • If set the attribute, then in other device without the problem, part of the text will be cut off. Can use [CheckedTextView](http://developer.android.com/reference/android/widget/CheckedTextView.html) as a replacement, and this link will be helpful: http://stackoverflow.com/questions/7989014/align-text-left-checkbox-right?rq=1 – cfh008 Jan 08 '15 at 03:43
1
<CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:layoutDirection="rtl"
        android:text="Check box One"
        android:textSize="12sp"
        android:textColor="@android:color/black"
        android:textAlignment="textStart"
        tools:ignore="MissingPrefix" />
Jafar Sadiq SH
  • 715
  • 9
  • 22
0

Try the following:

1) Your parent could have a default left padding causing it to leave some space.

2) You can manually set the correct orientation by setting the left margin to 0 with: android:layout_marginLeft

3) You can also set the padding to 0 if margin does not fix it by: android:paddingLeft

4) If none of these fix the problem, use a negative value for margin.

5) You can also try to use a Relative Layout to align the elements

6) Worst case, use absolute positioning

Harshit Jain
  • 331
  • 1
  • 6
0

Add

android:paddingLeft="0dp"

to avoid the empty space at the checkbox left side. The source link:

How to show android checkbox at right side?

answered by zeusboy.

Community
  • 1
  • 1
cfh008
  • 436
  • 5
  • 8
0

adding layoutdirection "rtl" works:

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right|center"//or "center_vertical" for center text
android:layoutDirection="rtl"
android:text="hello" />