Gaurav Gupta
Gaurav Gupta

Reputation: 4701

Equi-height row in table layout

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>

Upvotes: 0

Views: 1880

Answers (2)

Gaurav Gupta
Gaurav Gupta

Reputation: 4701

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).

Upvotes: 0

srbyk1990
srbyk1990

Reputation: 411

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>

Upvotes: 1

Related Questions