0

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

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.