androidandroid-layoutandroid-tablelayoutandroid-imagebutton

Arranging buttons with android: get rid of vertical space


I am new to android. I know layouts mainly from LaTex, but also at a layman's level from Swing, SWT and javafx.

As an exercise, I have tried a very simple layout for a calculator, which essentially boils down on having an array of buttons.

Whatever I try, there remains a vertical distance between rows of buttons.

I have changed the background color of the rows to see to whom the unwanted space belongs.

Screenshot:

I'm not an artist

Code (xml):

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="1dp"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="-28dp">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="1dp"
            android:background="#00ff00">

            <ImageButton
                android:id="@+id/imageButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:src="@drawable/img_a"
                tools:srcCompat="@tools:sample/avatars" />

            <ImageButton
                android:id="@+id/imageButton2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:src="@drawable/img_a"
                tools:srcCompat="@tools:sample/avatars" />

            <ImageButton
                android:id="@+id/imageButton3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:src="@drawable/img_a"
                tools:srcCompat="@tools:sample/avatars" />

            <ImageButton
                android:id="@+id/imageButton4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:src="@drawable/img_a"
                tools:srcCompat="@tools:sample/avatars" />

            <ImageButton
                android:id="@+id/imageButton5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:src="@drawable/img_a"
                tools:srcCompat="@tools:sample/avatars" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:background="@color/black">

            <ImageButton
                android:id="@+id/imageButton11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:src="@drawable/img_a"
                tools:srcCompat="@tools:sample/avatars" />

            <ImageButton
                android:id="@+id/imageButton12"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:src="@drawable/img_a"
                tools:srcCompat="@tools:sample/avatars" />

            <ImageButton
                android:id="@+id/imageButton13"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:src="@drawable/img_a"
                tools:srcCompat="@tools:sample/avatars" />

            <ImageButton
                android:id="@+id/imageButton14"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:src="@drawable/img_a"
                tools:srcCompat="@tools:sample/avatars" />

            <ImageButton
                android:id="@+id/imageButton15"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="0dp"
                android:layout_marginLeft="0dp"
                android:layout_marginTop="0dp"
                android:src="@drawable/img_a"
                tools:srcCompat="@tools:sample/avatars" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </TableLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

I want to get rid of the green and black space above and below the buttons which obviously belongs to the table rows.

How can I do that, and aside from the magic words I would like some understanding what and why went wrong.

Before I used table layout I have tried vertical layout with nested horizontal layouts, with more or less the same result.


Solution

  • You just need to set the padding for each ImageButton to 0 using android:padding="0dp" as it seems that the ImageButton default style to have this padding.

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <TableLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#00ff00">
    
                <ImageButton
                    android:id="@+id/imageButton1"
                    android:layout_width="wrap_content"
                    android:padding="0dp"
                    android:layout_height="wrap_content"
                    android:src="@drawable/img_a"
                    tools:srcCompat="@tools:sample/avatars" />
    
                <ImageButton
                    android:id="@+id/imageButton2"
                    android:layout_width="wrap_content"
                    android:padding="0dp"
                    android:layout_height="wrap_content"
                    android:src="@drawable/img_a"
                    tools:srcCompat="@tools:sample/avatars" />
    
                <ImageButton
                    android:id="@+id/imageButton3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="0dp"
                    android:src="@drawable/img_a"
                    tools:srcCompat="@tools:sample/avatars" />
    
                <ImageButton
                    android:id="@+id/imageButton4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="0dp"
                    android:src="@drawable/img_a"
                    tools:srcCompat="@tools:sample/avatars" />
    
                <ImageButton
                    android:id="@+id/imageButton5"
                    android:layout_width="wrap_content"
                    android:padding="0dp"
                    android:layout_height="wrap_content"
                    android:src="@drawable/img_a"
                    tools:srcCompat="@tools:sample/avatars" />
            </TableRow>
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/black">
    
                <ImageButton
                    android:id="@+id/imageButton11"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="0dp"
                    android:src="@drawable/img_a"
                    tools:srcCompat="@tools:sample/avatars" />
    
                <ImageButton
                    android:id="@+id/imageButton12"
                    android:layout_width="wrap_content"
                    android:padding="0dp"
                    android:layout_height="wrap_content"
                    android:src="@drawable/img_a"
                    tools:srcCompat="@tools:sample/avatars" />
    
                <ImageButton
                    android:id="@+id/imageButton13"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="0dp"
                    android:src="@drawable/img_a"
                    tools:srcCompat="@tools:sample/avatars" />
    
                <ImageButton
                    android:id="@+id/imageButton14"
                    android:layout_width="wrap_content"
                    android:padding="0dp"
                    android:layout_height="wrap_content"
                    android:src="@drawable/img_a"
                    tools:srcCompat="@tools:sample/avatars" />
    
                <ImageButton
                    android:id="@+id/imageButton15"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="0dp"
                    android:src="@drawable/img_a"
                    tools:srcCompat="@tools:sample/avatars" />
            </TableRow>
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
    
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </TableLayout>
    
    </androidx.constraintlayout.widget.ConstraintLayout>