How do I specify width and height of Android GridLayout cells in relative measures?

There’s two properties android:layout_columnWeight and layout_rowWeight which works like layout_weight in LinearLayout. This is supported in API 21. For older android devices, use the GridLayout from the v7 support library.

Those properties will allow you to adjust the width/height of column base on the value you assigned to each column. The formula goes like this (column_weight/ sum_of_column_weight) * gridLayout_width = column_width.

Here’s an example which is a gridview with 2 rows and 2 columns evenly spread out, each takes 50% of the weight and height of the grid.

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

    <TextView
        android:text="SEARCH FEE"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
     />

    <TextView
        android:layout_columnWeight="1"
        android:text="SEARCH FEE"
        android:layout_rowWeight="1"
     />

    <TextView
        android:text="SEARCH FEE"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        />

    <TextView
        android:layout_columnWeight="1"
        android:text="SEARCH FEE"
        android:layout_rowWeight="1"
        />
</GridLayout>

Leave a Comment