Search code examples
androidandroid-layoutandroid-tablelayoutandroid-layout-weight

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>

Solution

  • 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>