Android: How to make ImageButton have the same space in a GridLayout?

Viewed 67

Hey i need make this :

enter image description here

In a more equally spacing between the image. i mean to have same space between the 4 images in all axis like that: enter image description here

Note: the Kenny image used is just here for a dummy image source

Here is my xml file:

<?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">

    <androidx.gridlayout.widget.GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:columnCount="2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:rowCount="2">


        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@null"
            android:scaleType="fitCenter"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            app:srcCompat="@drawable/kenny" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@null"
            android:scaleType="fitCenter"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            app:srcCompat="@drawable/kenny" />

        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@null"
            android:scaleType="fitCenter"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            app:srcCompat="@drawable/kenny" />

        <ImageButton
            android:id="@+id/imageButton4"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@null"
            android:scaleType="fitCenter"
            app:layout_columnWeight="1"
            app:layout_rowWeight="1"
            app:srcCompat="@drawable/kenny" />

    </androidx.gridlayout.widget.GridLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

i searched a lot but cannot find a way to make what i want. Thanks

1 Answers

Add app:useDefaultMargins="true" and try providing margin to items.

Related