Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android gridview equal size cell

i am trying to make a timer application for practice, and i am running into an issue where the gridview doesn't have the same size as the others.

this is part of the code

 <GridLayout
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/passwordField"
    android:columnCount="3"
    android:rowCount="4"
    android:verticalSpacing="0dp"
    android:horizontalSpacing="0dp"
    android:stretchMode="columnWidth">


    <Button
        android:id="@+id/btnOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="0"
        android:layout_gravity="fill_horizontal"
        android:layout_row="0"
        android:onClick="addInput"
        android:text="@string/one" />

    <Button
        android:id="@+id/btnTwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="1"
        android:layout_gravity="fill_horizontal"
        android:layout_row="0"
        android:onClick="addInput"
        android:text="@string/two" />

    <Button
        android:id="@+id/btnThree"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_column="2"
        android:layout_gravity="fill_horizontal"
        android:layout_row="0"
        android:onClick="addInput"
        android:text="@string/three" /> </GridLayout>

there are more buttons after that, but this is all i needed to show right now as you can see from the image: enter image description here

it doesn't have the same cell size, where the 3rd Column is larger than the others. What should I do to fix this problem?

I had RelativeLayout before with everything in the correct place, but the button wasn't stretching out to fill the width.

like image 725
benji Wong Avatar asked Jul 04 '26 03:07

benji Wong


2 Answers

layout_columnWeight is the missing piece. Note that you can simplify the Button declarations as well:

<GridLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/passwordField"
        android:columnCount="3"
        android:rowCount="4"
        android:verticalSpacing="0dp"
        android:horizontalSpacing="0dp">


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="addInput"
            android:text="@string/one"
            android:layout_columnWeight="1"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:onClick="addInput"
            android:text="@string/two" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:onClick="addInput"
            android:text="@string/three" />

    </GridLayout>
like image 171
davehenry Avatar answered Jul 05 '26 20:07

davehenry


apply this attribute to each of your button xml

     android:layout_columnWeight="1"
like image 44
nikk Avatar answered Jul 05 '26 20:07

nikk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!