wbk727
wbk727

Reputation: 8408

Multiline textview in table row not wrapping properly

I'm trying to get my text of my text view within the table row to appear on more than one line, but for some reason it doesn't wrap properly and I end up with this (see screenshot). I know the text view has something to do this it but I don't know what I causing this problem to occur. What can be done to resolve this issue?

Screenshot

enter image description here enter image description here

List item XML

<?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">
    <TableRow>
        <ImageView
            android:id="@+id/img"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center_vertical" />

        <TextView
            android:id="@+id/txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            android:singleLine="false"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </TableRow>
</TableLayout>

Upvotes: 9

Views: 8342

Answers (4)

Sergey Shustikov
Sergey Shustikov

Reputation: 15821

You need specify android:layout_weight="1".

This value is telling to TableRow that the TextView is need to be equivalent to match_parent

<?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">
    <TableRow
            >
        <ImageView
                android:id="@+id/img"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="center_vertical"/>

        <TextView
                android:id="@+id/txt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginStart="15dp"
                android:layout_marginEnd="15dp"
                android:layout_weight="1"
                android:text="This is a long long long long text fodshfkjsdkjfjsh js hdfjksh kjdhf kjshdkjf"
                android:textAppearance="?android:attr/textAppearanceLarge"/>
    </TableRow>
</TableLayout>

enter image description here

Upvotes: 38

Nikul Rao
Nikul Rao

Reputation: 83

Try below code may help

<com.project_name.JustifiedTextView.JustifiedTextView
                    android:id="@+id/txtHide"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:padding="25dp"
                    xmlns:noghteh="http://noghteh.ir" 
                    android:text="" 
                    android:textColor="@color/BlueText"
                    android:textSize="@dimen/text_size" />

Link: https://github.com/navabi/JustifiedTextView

Upvotes: 0

Rustam
Rustam

Reputation: 6515

give <TableRow> attributes

   <TableRow
       android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/img"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center_vertical" />

        <TextView
            android:id="@+id/txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            android:singleLine="false"
            android:paddingRight="10dip"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </TableRow>

Upvotes: 0

Marcin D
Marcin D

Reputation: 916

Simple fix would be to increase margin end, try :

<TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginStart="15dp"
        android:layout_marginEnd="30dp"
        android:singleLine="false"
        android:textAppearance="?android:attr/textAppearanceLarge" />

Upvotes: -1

Related Questions