Column size on a Gridview on Android

Asked

Viewed 1,722 times

2

I have in my android project a gridview that I define as follows:

<GridView android:id="@+id/grid1"
android:layout_width="600dp"      
android:layout_height="fill_parent"
android:padding="5dp"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:numColumns="5"
android:columnWidth="120dp"
android:gravity="center"/>

I wonder if there is any way to set the gridview size to 100% of the screen and the screen size to 20% for each column.

  • Your layout is at 100% height as well as the gridview ? And what you need is to set the columns to 20% height, that’s it?

  • Not what I want is 20% width for each column

  • I assume what you’re looking for is gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH) since you have a fixed number of columns, you only need to indicate that they should occupy the entire width by the available size.

2 answers

1

Since you are indicating the number of columns, you can make use of android:stretchMode to indicate that you want all columns to have the same width depending on the space:

Defines how Columns should stretch to Fill the available Empty space, if any.

That translated:

Defines how columns should stretch to fill the available empty space that may exist.

You need to use the constant columnWidth indicating that each column is stretched equally:

android:stretchMode="columnWidth"

In your code:

<GridView android:id="@+id/grid1"
android:layout_width="600dp"      
android:layout_height="fill_parent"
android:padding="5dp"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:numColumns="5"
android:columnWidth="120dp"
android:stretchMode="columnWidth"
android:gravity="center"/>

For non-xml version, see gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH) (English).

0

The solution is simpler than it seems:

First: You should check if the LinearLayout that you are involving her GridView is with android:orientation="horizontal", If it’s not, you should put it on. Ps: If you’re not wrapping her in a LinearLayout(that would be out of standard) you must involve.

According to: You can only with a single attribute solve your problem, which would be the android:layout_weight="1"

You might ask yourself "But how did it get 20% the size of each column if you didn’t even set anything" - and then I answer you: Putting Weight 1 it automatically distributes in 5 items per "row" or horizontal we will always have 5 columns, and if you see:

(10/5itens) = 2 ----> 2 = 20% ----> 10 = 100%

Thus remaining:

<GridView android:id="@+id/grid1"
android:layout_width="0dp"
android:layout_weight="1"      
android:layout_height="fill_parent"
android:padding="5dp"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:numColumns="5"
android:columnWidth="120dp"
android:gravity="center"/>

Upshot:

GridView

Obs: Note that I put Width 0, because when we work with Weight, we should set the orientation size to 0, that is if the parent Linearlayout orientation was as vertical, the Weight would be applied vertically, then the Height would be 0.

For you to understand better, I recommend reading the my answer on another question

Browser other questions tagged

You are not signed in. Login or sign up in order to post.