0

I'm trying to make a GridLayout of 8x8 buttons (like a minesweeper). I'm managing the weights to all have the same width but it works fine until 4 buttons. When I add the 5th button in column 4 they expand the width full screen. This is my xml:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/GridLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:columnCount="8"
    android:orientation="horizontal"
    android:rowCount="8"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/b1"
        android:layout_column="0"
        android:layout_columnWeight="1"
        android:layout_row="0"
        android:text="B" />

    <Button
        android:id="@+id/b2"
        android:layout_column="1"
        android:layout_columnWeight="1"
        android:layout_row="0"
        android:text="B" />


    <Button
        android:id="@+id/b3"
        android:layout_column="2"
        android:layout_columnWeight="1"
        android:layout_row="0"
        android:text="B" />
    <Button
        android:id="@+id/b4"
        android:layout_column="3"
        android:layout_columnWeight="1"
        android:layout_row="0"
        android:text="B" />
</GridLayout>

Design with 4 button: 4 button

When I add the 5th button to the 4 column: button expands

Thank you!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
juanjovn
  • 492
  • 4
  • 8
  • go to this link which is similar to your problem https://stackoverflow.com/questions/10016343/gridlayout-not-gridview-how-to-stretch-all-children-evenly – vishal jangid Oct 25 '17 at 11:30

2 Answers2

0

Try creating layouts for various screen sizes

ldpi (low) ~120dpi
mdpi (medium) ~160dpi
hdpi (high) ~240dpi
xhdpi (extra-high) ~320dpi
xxhdpi (extra-extra-high) ~480dpi
xxxhdpi (extra-extra-extra-high) ~640dpi

i tried it on various screen sizes it worked for me, but the button expands if went out of the layout

Sandeep Manmode
  • 395
  • 4
  • 15
0

The problem is once the max width has reached there is no space for the fifth button so it occupies the entire row.

you have to maintain separate xml files it will fix your problem.