Search code examples
androidandroid-layoutandroid-tablelayout

How to decrease row height. (TableLayout>TableRow)


The height of the rows in this TableLayout is too much, higher than needed to allow for the text. I've played with a number of different values, but can't get it right. According to the docs, the default value for children of a TableLayout is wrap_content, but the row height appears to be more than that. Also it says that the value of the width is always wrap_content, so there's no point in setting that. By "children" does that mean only the direct children (TableRow in this case), or all underneath (TextView in this case)?

How do I decrease the row height?

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:shrinkColumns="*"
    android:stretchColumns="*"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1">
    <TextView
        android:id="@+id/textViewNameLabel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:typeface="sans"
        android:textSize="18sp"
        android:text="@string/name_string"
        android:gravity="start">
    </TextView>
    <TextView
        android:id="@+id/textViewName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:typeface="sans"
        android:textSize="18sp"
        android:text="Michael Foucalt"
        android:gravity="start">
    </TextView>
    <TextView
        android:id="@+id/textViewTitleLabel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:typeface="sans"
        android:textSize="18sp"
        android:text="@string/title_string"
        android:gravity="start"
        android:padding="10dp">
    </TextView>
    <TextView
        android:id="@+id/textViewTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:typeface="sans"
        android:textSize="18sp"
        android:text="Philosopher"
        android:gravity="start">
    </TextView>
</TableRow>

<TableRow
    android:id="@+id/tableRow2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1">
    <TextView
        android:id="@+id/textViewPhoneLabel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:typeface="sans"
        android:textSize="18sp"
        android:text="@string/phone_string"
        android:gravity="start">
    </TextView>
    <TextView
        android:id="@+id/textViewPhone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:typeface="sans"
        android:textSize="18sp"
        android:text="555.111.2222"
        android:gravity="start">
    </TextView>
    <TextView
        android:id="@+id/textViewFaxLabel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:typeface="sans"
        android:textSize="18sp"
        android:text="@string/fax_string"
        android:gravity="start"
        android:padding="10dp">
    </TextView>
    <TextView
        android:id="@+id/textViewFax"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:typeface="sans"
        android:textSize="18sp"
        android:text="555.123.4567"
        android:gravity="start">
    </TextView>
</TableRow>

<TableRow
    android:id="@+id/tableRow3"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1">
    <TextView
        android:id="@+id/textViewAddressLabel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:typeface="sans"
        android:textSize="18sp"
        android:text="@string/address_string"
        android:gravity="start">
    </TextView>
    <TextView
        android:id="@+id/textViewAddress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:typeface="sans"
        android:textSize="18sp"
        android:text="555 Champs Elysses"
        android:gravity="start">
    </TextView>
    <TextView
        android:id="@+id/textViewCityLabel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:typeface="sans"
        android:textSize="18sp"
        android:text="@string/city_string"
        android:gravity="start"
        android:padding="10dp">
    </TextView>
    <TextView
        android:id="@+id/textViewCity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:typeface="sans"
        android:textSize="18sp"
        android:text="Paris"
        android:gravity="start">
    </TextView>
</TableRow>
</TableLayout>

Here are images showing the current display and the desired display:

Current

Desired


Solution

  • Please remove:

    android:layout_weight="1"
    

    from the TableRow elements. It will cause the rows to be equally stretched along the length of the screen.

    EDIT The children in this instance would both be the TableRow and TextView. That's why doing something like this would be legal:

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TableRow android:id="@+id/tableRow1">
    
        <TextView
             android:id="@+id/textViewNameLabel"
             android:gravity="start"
             android:text="@string/name_string"
             android:textSize="18sp"
             android:textStyle="bold"
             android:typeface="sans" />
    
             .
             .
             .
    
        </TableRow>
    </TableLayout>
    

    They would all automatically be assigned wrap_content for their width and height attributes by wrapping in a TableLayout

    EDIT 2: Remove from all TextView components:

    android:padding="10dp"