I have 2 columns in my GridLayout
. What I want to do is make those columns take up half of the width of the screen each and then have its child contents fill their own cells width/height. I tried setting the children to fill_parent
but that just causes the first one to take over the entire layout. And it seems GridLayout doesn't support weight
? Maybe there is a better layout to use, but I want a Grid style layout so that seems like the natural choice.
This code is available on pre API21 with support library!
I have a simple piece of code to show 4 buttons in a gridLayout of 2 columns that take 50% of the available space: perhaps it can help
<GridLayout android:id="@+id/grid" android:layout_width="match_parent" android:layout_height="wrap_content" android:columnCount="2" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:layout_gravity="fill" android:layout_columnWeight="1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:layout_gravity="fill" android:layout_columnWeight="1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:layout_gravity="fill" android:layout_columnWeight="1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" android:layout_gravity="fill" android:layout_columnWeight="1" /> </GridLayout>
Solution is perhaps this :
android:layout_gravity="fill" android:layout_columnWeight="1"
For pre API 21, use support library:
add
compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:design:23.1.1'
to your dependencies.
Then in your xml file:
<android.support.v7.widget.GridLayout xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" app:columnCount="2" app:orientation="horizontal" app:rowCount="1"> <TextView android:text="1" android:textStyle="bold" app:layout_columnWeight="1" /> <TextView android:text="2" android:textStyle="bold" app:layout_columnWeight="1" /> </android.support.v7.widget.GridLayout>
Here note the use of "app" prefix and dont forget to add
xmlns:app="http://schemas.android.com/apk/res-auto"
to your xml file
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