Search code examples
androidandroid-layoutheightwidthandroid-gridlayout

Fill whole screen with four buttons by two rows in Android


I want to fill the whole screen with four buttons that has the same width and height so I thought a grid layout would be a great idea:

<GridLayout
    android:id="@+id/grid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="2"
    android:rowCount="2"
    >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_columnWeight="1"
        android:layout_rowWeight="0"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_columnWeight="1"
        android:layout_rowWeight="0"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        />

</GridLayout>

Unfortunatly my result looks like this:

GridLayout Results

I don't understand why the height of the second row is so big?
I thought every cell in a grid layout got the same width and height!


Solution

  • This can also be done with ConstraintLayout, which will give you a flat hierarchy and more flexibility for future changes.

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/button"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:text="Button"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/button2"
            app:layout_constraintBottom_toTopOf="@id/button3"/>
    
        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:text="Button"
            app:layout_constraintStart_toEndOf="@+id/button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/button4" />
    
        <Button
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:text="Button"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/button4"
            app:layout_constraintTop_toBottomOf="@+id/button" />
    
        <Button
            android:id="@+id/button4"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:text="Button"
            app:layout_constraintTop_toBottomOf="@id/button2"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/button3"/>
    </android.support.constraint.ConstraintLayout>
    

    enter image description here