27

I am working on a custom list view. I want to show a CheckBox at the custom view. There is no text for the CheckBox. I found it always have some spaces at the right of the CheckBox.

Here is my layout xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:background="#fa9654"
android:paddingTop="65dp" android:paddingBottom="65dp">



<TextView android:id="@+id/bus_route_list_item_num"
    android:layout_height="wrap_content" android:layout_width="0dip"
    android:gravity="center" android:layout_gravity="center_vertical|center_horizontal"
    android:layout_weight="0.15"></TextView>

<TextView android:id="@+id/bus_route_list_item_station"
    android:layout_height="wrap_content" android:layout_width="0dip"
    android:gravity="left" android:layout_gravity="center_vertical|center_horizontal"
    android:layout_weight=".5"></TextView>


<TextView android:id="@+id/bus_route_list_item_fee"
    android:layout_height="wrap_content" android:layout_width="0dip"
    android:gravity="center" android:layout_gravity="center_vertical|center_horizontal"
    android:layout_weight=".15"></TextView>


<CheckBox android:id="@+id/bus_route_list_item_reminder" android:layout_height="wrap_content" 
    android:layout_width="0dip" android:layout_weight=".20" android:gravity="center" 
    android:layout_gravity="center" android:paddingRight="0dp" android:paddingLeft="0dp" 
    android:paddingTop="0dp" android:paddingBottom="0dp" android:background="#0066ff" 
    android:text=""
/>

</LinearLayout>

The result looks like:

Checkbox result

As you can see there are some space at the right of the checkbox. What I want is put the checkbox at the middle of the blue area.

Is it possible to remove the unwanted space? thanks

Edric
  • 24,639
  • 13
  • 81
  • 91
mobile app Beginner
  • 1,651
  • 3
  • 26
  • 40

6 Answers6

48

by default, the Checkbox has minWidth and minHeight value

enter image description here

you can set its value to 0

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minWidth="0dp"
    android:minHeight="0dp" />

The result will be like that without any extra spaces

enter image description here

Anwar SE
  • 743
  • 2
  • 7
  • 14
19

You can wrap CheckBox in LinearLayout and then use android:gravity="center" on that layout.

 <LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="0dip" 
    android:layout_weight=".20"              
    android:background="#ff0000" 
    android:gravity="center">

    <CheckBox android:id="@+id/bus_route_list_item_reminder" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"                     
    />

</LinearLayout>

As another alternative, you can use RelativeLayout. This would greatly simplify you layout and you will be able to get rid of layout_weight.

inazaruk
  • 74,247
  • 24
  • 188
  • 156
12

Neither of previous solutions worked for me, but I've tried applying a translation to the content and it worked pretty well, no need in additional layout hierarchy, nor implementing own views:

                <CheckBox
                ...
                android:text="@null"
                android:translationX="12dp" />

Also, it keeps bounds of the element in proper place, so touch area is not shifted.

Pavlus
  • 1,651
  • 1
  • 13
  • 24
7

The translationX seems to work. But it causes problem if you want to support RTL layouts. Another solution would be to set the width of checkbox to a fixed length (e.g. 26dp):

<CheckBox
    android:layout_width="26dp"
    android:layout_height="wrap_content"
    android:text="@null" />
Mahozad
  • 18,032
  • 13
  • 118
  • 133
4

To remove extra space at right of the image (when there is no text) extend CheckBox class and override getSuggestedMinimumWidth() method in order to return there image width. Complete solution:

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.CheckBox;

public class CheckBoxWithoutText extends CheckBox
{
    private Drawable buttonDrawable;

    public CheckBoxWithoutText(Context context)
    {
        super(context);
    }

    public CheckBoxWithoutText(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    @Override
    protected int getSuggestedMinimumWidth()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
        {
            return getCompoundPaddingLeft() + getCompoundPaddingRight();
        }
        else
        {
            return buttonDrawable == null ? 0 : buttonDrawable.getIntrinsicWidth();
        }
    }

    @Override
    public void setButtonDrawable(Drawable d)
    {
        buttonDrawable = d;
        super.setButtonDrawable(d);
    }
}
Ruslan Yanchyshyn
  • 2,774
  • 1
  • 24
  • 22
1

I would use a relative layout here. Aligning checkbox on parent right...

Regards, Stéphane

Snicolas
  • 37,840
  • 15
  • 114
  • 173