5

I am using image for checkbox. This is the xml i am uisng,

<CheckBox
    android:id="@+id/remember"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"        
    android:layout_below="@+id/tableLayout1"
    android:button="@drawable/checkbox_selector"
    android:layout_marginLeft="52dp"
    android:text="@string/remember"
    android:textColor="#000000" />

I am getting the gap between the image and text ( see in the below image), is it default; is it possible to change if yes let me know what property i have to add.

checkbox_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" 
    android:drawable="@drawable/checked" />
<item android:state_checked="false" 
    android:drawable="@drawable/un_checked" />
</selector>

enter image description here

code_fish
  • 3,381
  • 5
  • 47
  • 90
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
  • Which layout you are using? assuming you are using relative (or) linear layout, check what is the padding you are using. By adjusting padding I guess you can fix this. – kosa Feb 10 '12 at 04:30
  • i am using relative layout , but there is no padding for that layout.. – RajaReddy PolamReddy Feb 10 '12 at 04:33
  • add the definition for the textview – Nick Campion Feb 10 '12 at 04:40
  • @NickCampion Still i am getting the gap on top and right.. – RajaReddy PolamReddy Feb 10 '12 at 04:52
  • Honestly, the best way to diagnose these issues is to open the app on your emulator and connect with the hierarchy viewer. You can then see by click the elements and toggling on and off the "extras" who is contributing extra spacing to your views. My guess is that there are two issues. 1) Text views align on a text base baseline while your checkbox is aligning based on the center of the view and 2) text view might be getting skewed by gravity or default text view padding. Again, the easiest way to validate all this is through the hierarchyviewer tool built into your eclipse. – Nick Campion Feb 10 '12 at 06:12

3 Answers3

7

Try messing with the android:paddingLeft property of the checkbox.

See this post for further information: Android - Spacing between CheckBox and text

Community
  • 1
  • 1
Dan
  • 724
  • 1
  • 9
  • 19
2

Use below code to close the gap

 final float scale = this.getResources().getDisplayMetrics().density;
 checkBox.setPadding(checkBox.getPaddingLeft() - (int)(10.0f * scale + 0.5f),
    checkBox.getPaddingTop(),
    checkBox.getPaddingRight(),
    checkBox.getPaddingBottom());
0

Please try this one:::::

    <RelativeLayout  android:layout_width="wrap_content"
           android:layout_height="wrap_content" 
           android:gravity="center">
         <CheckBox
             android:id="@+id/checkBox1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:button="@drawable/checkbox_selector"
             android:text="" />
         <TextView
             android:id="@+id/textView1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="Remember Me"
             android:layout_toRightOf="@+id/checkBox1"
             android:layout_centerInParent="true
             android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
mainu
  • 448
  • 2
  • 11