Android checkbox size and alignment

40 Views Asked by At

I have a simple default checkbox in android app:

<CheckBox
         android:id="@+id/sklad"
         android:layout_below="@id/barcode_text"
         android:layout_toLeftOf="@+id/skladlabel"
         android:layout_centerVertical="true"
         android:gravity="center"
         android:checked="true"
         android:layout_width="48dp"
         android:layout_height="48dp"
         />

However I have problems with alignment of the checkbox - as you can see, it is not centered in its view. I set the layout width to 48dp, because if I leave it as wrap_content, it says touch size too small.

So with this it looks good:

android:layout_width="wrap_content"
android:layout_height="wrap_content"

But with 48dp width it looks like this:

enter image description here

So I just need to center it in its view and that way it will be closer to the text.

2

There are 2 best solutions below

2
Emre B On

You can't change CheckBox's button size (yes it is button property) by changing the layout's width and height. Maybe you can use scaleX and scaleY properties but they may not give you what you actually want.

Maybe you should consider using a custom checkbox.

See here: Custom checkbox image android

0
Darksymphony On

ok, so I solved it by adding negative margin. Not a proper solution, but it keeps the touch size 48dp and it is aligned next to the label android:layout_marginRight="-18dp"

Now the area is overlapping, but it doesn't really matter as the checkbox is selectable and touch size is kept. In preview:

enter image description here

In reality:

enter image description here