jmease
jmease

Reputation: 2535

Android GridView Formatting

I have a GridView that I want to be 4 equally sized TextViews in a 2 x 2 GridView. I want the TextViews to be centered within their respective quadrant of the GridView.

My first issue is that nothing is centering. Everything gravitates to the left. My second is that while I can get the column width to size to 50% of the available width, I can't figure out how to get the row height to adjust to 50% of available height. Weight only seems to work one way.

<GridView  
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/MyGrid"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:padding="5dp"
   android:verticalSpacing="5dp"
   android:horizontalSpacing="10dp"
   android:numColumns="2"
   android:columnWidth="400dp"
   android:stretchMode="columnWidth"
   android:layout_weight=".5"
   android:gravity="center">  
</GridView>

Portrait View enter image description here

Landscape View enter image description here

Upvotes: 0

Views: 1491

Answers (1)

Matthew
Matthew

Reputation: 5202

What you maybe want is to use a TableLayout instead. The GridView is similar to a ListView that has items, but in this case, you want to have only 4 items rather than an indefinite amount.

GridView

GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid. The grid items are automatically inserted to the layout using a ListAdapter.

TableLayout

TableLayout is a ViewGroup that displays child View elements in rows and columns.

Here is a snippet of xml that will work:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/MyGrid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight=".5"
    android:gravity="center"
    android:padding="5dp"
    android:stretchColumns="0,1" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </TableRow>

</TableLayout>

Upvotes: 1

Related Questions