antonio Musella
antonio Musella

Reputation: 534

How can I set the height of GridView cell based on screen height?

In my layout I have only eight cells.
I want the cell divide all the space available on the screen. Is this possible?

This is my gridview layout:

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dataGrid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10px"
    android:verticalSpacing="10px"
    android:horizontalSpacing="10px" 
    android:numColumns="2"
    android:gravity="fill" />

And this is the item layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:padding="6dip"  >   
  <TableRow
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
      <TextView
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:textColor="#E00000"
          android:textStyle="bold"
          android:gravity="left"
          android:textSize="16dip"/>    
      <TextView
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:textColor="#000000"
          android:textStyle="normal"
          android:gravity="right"
          android:textSize="12dip"/>
    </LinearLayout>
  </TableRow>
</LinearLayout>

Upvotes: 9

Views: 15226

Answers (3)

happydude
happydude

Reputation: 3889

Unfortunately, GridView is a tricky layout to manipulate to get internal views to fit a specific size like you are attempting. I think it is more appropriate to think of GridView as an advanced ListView for showing variable-length, scrollable lists like thumbnails in a photo album, as opposed to a "grid." I started down this route when I originally wanted to make a fixed sized grid for a game, but it is really not an appropriate layout for that at all.

Since your grid is always 8 cells, might I suggest using nested LinearLayouts (or if you don't want all the cells exactly the same size, the more complex TableLayout / TableRow combo) instead:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:weightSum="1.0"  >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="0dp" 
        android:layout_weight="0.5"
        android:orientation="horizontal"
        android:weightSum="1.0" >

        <!-- Drop in 4 items here with layout_weight of 0.25 -->

    </LinearLayout>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="0dp" 
        android:layout_weight="0.5"
        android:orientation="horizontal"
        android:weightSum="1.0" >

        <!-- Drop in 4 items here with layout weight of 0.25 -->

    </LinearLayout >
</LinearLayout >

This would make a fixed 2x4 grid that always exactly fills the screen.

Upvotes: 1

Trung Nguyen
Trung Nguyen

Reputation: 7532

You should try [android:stretchMode][1]

[1]: http://developer.android.com/reference/android/widget/GridView.html#attr_android:stretchMode : Defines how columns should stretch to fill the available empty space, if any.

Upvotes: 0

Unconn
Unconn

Reputation: 622

First, Use PS instead of PX. This will should allow it to scale to different DPI devices.

Second, to get each cell to be even size you could put a tableView in each cell that contains a clear image. Set the size of the image with PS (not px). The table view should allow you to layer the layout ojects in each cell and the clear graphic should keep all of the cells the same size.

I would also remove any padding.

Let us know.

Upvotes: 0

Related Questions