35

Is it really not possible to easily force equal column widths in an Android GridLayout? Trying the obvious layout...

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount="4"
    android:rowCount="8"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_gravity="center"
        android:text="Button 1" />
    <Button
        android:id="@+id/button2"
        android:layout_gravity="center"
        android:text="Button 2" />
    <Button
        android:id="@+id/button3"
        android:layout_gravity="center"
        android:text="Button 3" />
    <Button
        android:id="@+id/button4"
        android:layout_gravity="center"
        android:text="Button 4" />

</GridLayout>

... leaves the rightmost button centered in a column that is clearly wider than the other three. (Play with the button text and you can easily get worse examples.)

I read the Android documentation regarding excess space distribution in GridLayouts, but it seems so obvious that even column widths are often (any maybe even typically) wanted in a situation like this that I have to believe I'm missing something obvious/easy.

Boulder Keith
  • 535
  • 1
  • 5
  • 10
  • Try to read this post about using GridLayout, can be helpful http://android-developers.blogspot.co.nz/2011/11/new-layout-widgets-space-and-gridlayout.html – GhostDerfel Mar 14 '14 at 19:22

2 Answers2

42

Just use column weight for GridLayout's children:

android:layout_width="0dp"
android:layout_columnWeight="1"

Remember using support library if your project's MinSdk < API 21:

implementation 'com.android.support:appcompat-v7:27.1.1' 
implementation 'com.android.support:gridlayout-v7:27.1.1'

You should use android.support.v7.widget.GridLayout instead of GridLayout. And don't forget using app namespace when using support library:

xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="0dp"
app:layout_columnWeight="1"

UPD:

If you make your GridLayout programmatically, use GridLayout.Spec. For example:

GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams(
    GridLayout.spec(GridLayout.UNDEFINED, 1f),
    GridLayout.spec(GridLayout.UNDEFINED, 1f)
);

layoutParams.width = 0;

yourChildView.setLayoutParams(layoutParams);
yourGridLayout.addView(yourChildView);

(for every child view)

Manaus
  • 407
  • 5
  • 9
kissed
  • 715
  • 6
  • 7
30

Use a table layout instead and specify stretch columns.

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textViewLeaveRemainingDescription"
    android:stretchColumns="0,1">

    <TableRow>
        <Button
            android:id="@+id/button1"
            android:layout_gravity="center"
            android:text="Button 1" />

        <Button
            android:id="@+id/button3"
            android:layout_gravity="center"
            android:text="Button 3" />
    </TableRow>

    <TableRow>
        <Button
            android:id="@+id/button2"
            android:layout_gravity="center"
            android:text="Button 2" />

        <Button
            android:id="@+id/button4"
            android:layout_gravity="center"
            android:text="Button 4" />
    </TableRow>
</TableLayout>
nikib3ro
  • 20,366
  • 24
  • 120
  • 181
Al Polden
  • 890
  • 11
  • 25