Jimmy
Jimmy

Reputation: 304

TextView not appearing correctly in Table layout

The textview is not displaying text correctly. The text spans two lines, but the second line of text is only showing the text's upper half, as if the text is cut into 2 halves horizontally, despite using height = wrap_content.

xml code:

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="15dp"
        android:weightSum="1" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.4"
            android:text="No. of direct Reportees:" />

        <EditText
            android:id="@+id/direct"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.6"
            android:hint="Eg. 12"
            android:inputType="numberDecimal"
            android:textAppearance="?android:attr/textAppearanceSmall" >

            <requestFocus />
        </EditText>
    </TableRow>

SOLVED:

This behavior is due to baseline alignment. The container has the correct height (it is the max of its two children) but the Textview is shifted down to be baseline aligned with the button. This behavior cannot be changed to preserve layouts in existing apps. The correct way to implement this layout in your case is to add android:baselineAligned="false" on the LinearLayout tag. This will also get rid of the extra vertical space above the TextView.

Upvotes: 0

Views: 1379

Answers (4)

Jimmy
Jimmy

Reputation: 304

This behavior is due to baseline alignment. The container has the correct height (it is the max of its two children) but the Textview is shifted down to be baseline aligned with the button. This behavior cannot be changed to preserve layouts in existing apps. The correct way to implement this layout in your case is to add android:baselineAligned="false" on the LinearLayout tag. This will also get rid of the extra vertical space above the TextView.

Upvotes: 0

Kanwaljit Singh
Kanwaljit Singh

Reputation: 4377

Try this-

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.4"
        android:text="No. of direct Reportees:" />

    <EditText
        android:id="@+id/direct"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.6"
        android:hint="Eg. 12"
        android:inputType="numberDecimal"
        android:textAppearance="?android:attr/textAppearanceSmall" >

        <requestFocus />
    </EditText>
</TableRow>

Source-

http://www.chess-ix.com/blog/the-use-of-layout_weight-with-android-layouts/

Upvotes: 0

Nambi
Nambi

Reputation: 12042

Put the TextView and EditText inside the LinearLayout to make the weight attribute work:

<TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="15dp">
<LinearLayout
        android:id="@+id/table_row_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/textView1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.4"
            android:text="No. of direct Reportees:" />

        <EditText
            android:id="@+id/direct"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.6"
            android:hint="Eg. 12"
            android:inputType="numberDecimal"
            android:textAppearance="?android:attr/textAppearanceSmall" >

            <requestFocus />
        </EditText>
</LinearLayout>
</TableRow>

Upvotes: 1

vipul mittal
vipul mittal

Reputation: 17401

I think height is assigned to the text view before width.

As in because of weight, width is calculated at the time of rendering.

So by the time width is assigned textview's height was set.

I think if text in that textview is going to be static try giving it some hard coded height.

Upvotes: 0

Related Questions