Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spacing for checkmark image of the check box in android

Hi i am trying to create a check box list which should looks like in the below image which will have space before and after checkmark image.

enter image description here

but when i am trying to create check box its is showing like the below image.

enter image description here

there is no space surrounds to check-mark image like i am showing the first image. If i use padding to check box component its like "android:padding=50dp" its giving space between check-mark image and text. But not giving space in the starting .i.e before Check-Mark image. below is my xml layout for check box

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="15dp">
<CheckBox android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:button="@drawable/btn_check_square"
       android:background="@drawable/filter_item_background"
       android:textColor="@android:color/black"
       android:padding="5dp"
       android:text="check box text"/>
</LinearLayout>

Any help to solve this issue is appreciated.

like image 384
Raj Avatar asked Jan 30 '13 06:01

Raj


4 Answers

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" />
like image 100
Raj Avatar answered Nov 01 '22 10:11

Raj


Try this (without custom graphics):

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableLeft="@android:color/transparent"
    android:drawablePadding="8dp"
    android:text="CheckBox" />

It uses a little trick: As Checkbox is derived from Button, we can add a drawable and can now set a padding between label and drawable. As we do not acutally need a real icon, we use the transparent color instead.

like image 41
Ridcully Avatar answered Nov 01 '22 10:11

Ridcully


<CheckBox
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/filter_item_background"
    android:button="@null"                             //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" />
like image 29
Venkatesh S Avatar answered Nov 01 '22 12:11

Venkatesh S


Use the below code

final float scale = this.getResources().getDisplayMetrics().density;
checkbox.setPadding(checkbox.getPaddingLeft()
        + (int) (10.0f * scale + 0.5f),
        checkbox.getPaddingTop(),
        checkbox.getPaddingRight(),
        checkbox.getPaddingBottom());
like image 1
Sunny Avatar answered Nov 01 '22 11:11

Sunny