Search code examples
javaandroidxmlgrid-layout

How to change the Button size in a GridLayout to "match_parent"?


I currently have this:

enter image description here

And I'm trying to get this:

enter image description here

I have tried for over an hour now, but cannot seem to get it working. Please take a look at my XML below.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
android:background="@drawable/png1"
    android:layout_height="match_parent">

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:text="Button"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:id="@+id/button1" />

        <Button
            android:text="Button"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:id="@+id/button2" />

        <Button
            android:text="Button"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:id="@+id/button3"
            android:layout_row="1"
            android:layout_column="1" />
        <Button
            android:text="Button"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:id="@+id/button4"
            android:layout_row="1"
            android:layout_column="0" />
        <Button
            android:text="Button"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:id="@+id/button5"
            android:layout_row="2"
            android:layout_column="0" />


    </GridLayout>
</LinearLayout>

How can i change the last Button that he takes the whole place, like "match_parent"? Thanks


Solution

  • Add android:layout_columnSpan="2" to android:id="@+id/button5". This will make the button span 2 columns.

       <Button
            android:text="Button"
            android:layout_columnSpan="2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:id="@+id/button5"
            android:layout_row="2"
            android:layout_column="0" />