AndroidDev
AndroidDev

Reputation: 16395

Table Layout with equal rows height

I have created a TableLayout where I want each row to have the same height, regardless of it's content. How can I do this?

Here is my code. Each row element get the same width, but the heights are not equal. What am I doing wrong?

 <TableLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"                    android:background="@color/grey"
                android:padding="1dip" >

                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="@color/grey" >

                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:layout_marginTop="2dip"
                            android:layout_weight="1"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_make"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carMake"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:layout_gravity="center_vertical"
                            android:layout_marginLeft="1dip"
                            android:layout_weight="1"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_detail" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:layout_marginTop="2dip"
                            android:layout_weight="1"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_model"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carModel"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:layout_gravity="center_vertical"
                            android:layout_weight="1"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_detail" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:layout_marginTop="2dip"
                            android:layout_weight="1"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_version"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carYear"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:layout_gravity="center_vertical"
                            android:layout_weight="1"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_year" />
                    </LinearLayout>
                </TableRow>

                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_marginTop="1dip"
                    android:layout_weight="1"
                    android:background="@color/grey" >

                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:layout_marginTop="2dip"
                            android:background="@color/white"
                                android:gravity="center_horizontal"
                            android:text="@string/car_year"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carYear"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_year" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:layout_marginTop="2dip"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_mileage"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carYear"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_year" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:layout_marginTop="2dip"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_engine_capacity"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carYear"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:layout_gravity="center_vertical"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_year" />
                    </LinearLayout>
                </TableRow>

                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_marginTop="1dip"
                    android:layout_weight="1"
                    android:background="@color/grey" >

                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_engine_type"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carYear"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_year" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_exterior_color"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carYear"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_year" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_city"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carYear"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_year" />
                    </LinearLayout>
                </TableRow>

                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_marginTop="1dip"
                    android:layout_weight="1"
                    android:background="@color/grey" >

                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_transmittion"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carYear"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_year" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_area"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carYear"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_year" />
                    </LinearLayout>

                        <LinearLayout
                        android:layout_width="0dip"
                        android:layout_height="fill_parent"
                        android:layout_weight="1"
                        android:background="@color/white"
                        android:orientation="vertical" >

                        <TextView
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_marginBottom="5dip"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_registered_city"
                            android:textStyle="bold" />

                        <TextView
                            android:id="@+id/carYear"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:background="@color/white"
                            android:gravity="center_horizontal"
                            android:text="@string/car_year" />
                    </LinearLayout>
                </TableRow>
            </TableLayout>

Upvotes: 18

Views: 25948

Answers (6)

Gautam Vasoya
Gautam Vasoya

Reputation: 879

Below Answer Deprecated Now :

You can Change in your TableRow -> LinearLayout

<LinearLayout
                    android:layout_width="0dip"
                    android:layout_height="wrap_content"    
                    android:layout_weight="1"
                    android:background="@color/white"
                    android:orientation="vertical" >

Upvotes: -3

plsplsme
plsplsme

Reputation: 101

I just came across this issue and solved it. It turns out Sunny's answer worked for me.

Though as mentioned in the comment, I could figure out if it works by just reading it. I am a newbee so cannot neither leave comment nor vote it to useful. Therefore I am posting it here.

The idea is that you let TableRow to decide the height of the row. By setting layout_height to wrap_content, the height of the row is depends on the contents which is TextView.

Since the border line of the cell would be the boundary of TextView, the layout_height should be match_parent.

I wish it is helpful for Android newbee (yes i am new to android too).

Upvotes: 0

Learn OpenGL ES
Learn OpenGL ES

Reputation: 4950

Introduced in API 11, you can set android:measureWithLargestChild="true" on the TableLayout and android:layout_weight="1" on each TableRow.

Upvotes: 11

Sunny Jain
Sunny Jain

Reputation: 39

here is a solution:

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

       <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_gravity="fill"
        />
       <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_gravity="fill"
        />
    </TableRow>
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

       <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_gravity="fill"
        />
       <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_gravity="fill"
        />
    </TableRow>

Upvotes: 3

Nipun Gogia
Nipun Gogia

Reputation: 1856

here is your solution :

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

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="4" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#B0B0B0" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:orientation="vertical"
            android:weightSum="4" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#B0B0B0" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:orientation="vertical"
            android:weightSum="4" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#B0B0B0" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:orientation="vertical"
            android:weightSum="4" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#B0B0B0" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:orientation="vertical"
            android:weightSum="4" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#B0B0B0" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="1dp"
            android:orientation="vertical"
            android:weightSum="4" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:background="#B0B0B0" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="#B0B0B0" />
        </TableRow>
    </TableLayout>

</LinearLayout>

enter image description here

Upvotes: 4

TieDad
TieDad

Reputation: 9929

<TableRow
                android:layout_width="fill_parent"
                android:layout_height="xxdip"  // HERE define a fixed size

Upvotes: -2

Related Questions