0

I want to make a re-usable table layout, in which I have 3 rows and last row contains 1 element only whereas first two contains 2 element each.

Now, problem is, rows in the table layout are not of equal height despite of giving equal weight to each row.

Bottom row is consuming too much space.

Following is the layout content:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="3"
    >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="2" >

        <TextView
            android:id="@+id/txtvw_age"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/age" >
        </TextView>

        <TextView
            android:id="@+id/txtvw_roll_no"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/roll_no" >
        </TextView>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="2" >

        <TextView
            android:id="@+id/txtvw_standard"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/standard" >
        </TextView>

        <TextView
            android:id="@+id/txtvw_section"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/section" >
        </TextView>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        >

        <TextView
            android:id="@+id/txtvw_ct_name"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="@string/ct_name" >
        </TextView>
    </TableRow>
</TableLayout>
double-beep
  • 5,031
  • 17
  • 33
  • 41
Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72
  • Any height and width settings for `TableRow` simply is ignored, and `fill_parent` is set to `layout_width` and `wrap_content` to `layout_height` by `TableLayout` automatically – frogatto Mar 05 '14 at 21:58

2 Answers2

1

try this:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="3"
    >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="2" >

        <TextView
            android:id="@+id/txtvw_age"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/save" >
        </TextView>

        <TextView
            android:id="@+id/txtvw_roll_no"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/save" >
        </TextView>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="2" >

        <TextView
            android:id="@+id/txtvw_standard"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/save" >
        </TextView>

        <TextView
            android:id="@+id/txtvw_section"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/save" >
        </TextView>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <TextView
            android:id="@+id/txtvw_ct_name"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="@string/save" >
        </TextView>
    </TableRow>
</TableLayout>
srbyk1990
  • 411
  • 5
  • 17
  • 1
    Your changes seems to be working fine, but i am unable to understand how these changes are working. It would be nice if you add little description too. However, i gotta one more solution and will put that in answer too. – Gaurav Gupta Mar 06 '14 at 06:34
0

I made the following change to the last TableRow and all goes well

<TableRow
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"  
    android:weightSum="1"   
    >

    <TextView
        android:id="@+id/txtvw_ct_name"
        android:layout_width="0dp"      // this t.v will consume all width
        android:layout_weight="1"
        android:layout_height="match_parent"  // consume all height as of parent which is adjusted by weight.
        android:text="@string/save" 
        >
    </TextView>
</TableRow>

Since table layout has weight sum 3, i provide equal weight to the table row ( w.r.t height).

Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72