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.
but when i am trying to create check box its is showing like the below image.
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.
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" />
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.
<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" />
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With