Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 481
antonio Musella Avatar asked Feb 25 '11 17:02

antonio Musella


1 Answers

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.

like image 183
happydude Avatar answered Oct 06 '22 01:10

happydude