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:

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.
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>
apply this attribute to each of your button xml
android:layout_columnWeight="1"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With