How to remove padding around Android CheckBox

AndroidCheckboxMarginPadding

Android Problem Overview


I need to put check to the right hand top corner of my imageview. But when I do this I noticed a default margin around my checkbox. Is there a way to remove this??

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" >

        <ImageView
            android:id="@+id/thumbImage"
            android:layout_width="100dp"
            android:layout_height="132dp"
            android:layout_gravity="center" />
    </FrameLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="#ff0000"
        android:gravity="center" >

        <CheckBox
            android:id="@+id/itemCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_gravity="center"
            android:button="@drawable/checkbox_background"
            android:paddingLeft="0dp"
            android:paddingTop="0dp" />

    </LinearLayout>

</RelativeLayout>

Android Solutions


Solution 1 - Android

There's now a better way of doing this:

android:minWidth="0dp"
android:minHeight="0dp"

Solution 2 - Android

To remove radio button default margins or padding in android

if you using xml

android:minWidth="0dp"
android:minHeight="0dp"

if using programmatically

 radiobtn.setMinimumHeight(0);
 radiobtn.setMinimumWidth(0);

Solution 3 - Android

You could use a negative margin.

android:layout_marginTop = "-5dp"
android:layout_marginRight = "-5dp"

Solution 4 - Android

This works for me in Constraint Layout. I hope it will work for another layout.

<androidx.appcompat.widget.AppCompatCheckBox
                ...
                android:translationX="-5dp"
                 />

Solution 5 - Android

If you look at the source code for the CompoundButton class that is extended by the Checkbox class at the onDraw() method, the padding depends from the gravity

  @Override
    protected void onDraw(Canvas canvas) {
        ...

            final int top;
            switch (verticalGravity) {
                case Gravity.BOTTOM:
                    top = getHeight() - drawableHeight;
                    break;
                case Gravity.CENTER_VERTICAL:
                    top = (getHeight() - drawableHeight) / 2;
                    break;
                default:
                    top = 0;
            }
       ...
    }

So what you can do is set the xml value for the attribute gravity to
android:gravity="top|start"

But this can be used only if you don't want to use text with your checkbox, since the gravity is used to center the text.

Solution 6 - Android

Thanks to @VladislavShcherbakov comment, it is:

android:paddingLeft="-5dp"
android:layout_marginStart="-5dp"
android:layout_marginLeft="-5dp"
// android:translationX="-5dp"

Solution 7 - Android

Why to complicate with a negative padding and all. Use the simple and straight way, give padding to the FrameLayout instead tof the top most Relative layout that would do the trick. As you have only the CheckBox in the second layout there is no need to give padding for it.

Please do let me know if you face any problems or have any further doubts

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLincyView Question on Stackoverflow
Solution 1 - AndroidRishabh876View Answer on Stackoverflow
Solution 2 - AndroidAbhishek GargView Answer on Stackoverflow
Solution 3 - AndroidBenito BertoliView Answer on Stackoverflow
Solution 4 - AndroidShihab UddinView Answer on Stackoverflow
Solution 5 - AndroidslaviboyView Answer on Stackoverflow
Solution 6 - AndroidCoolMindView Answer on Stackoverflow
Solution 7 - AndroidpvnView Answer on Stackoverflow