9

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>
wbk727
  • 8,017
  • 12
  • 61
  • 125

4 Answers4

38

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

Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119
  • 4
    I think if you use `weight`, the `layout_width` should be `0dp`. – justHooman Aug 13 '15 at 18:03
  • In case you embed your views in a `LinearLayout` in the `TableRow` you need to set the `android:layout_weight="1"` on the `LinearLayout` only and on the `TextView` set `android:layout_width="match_parent"` – Bruno Bieri Oct 31 '19 at 08:52
0

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>
Rustam
  • 6,485
  • 1
  • 25
  • 25
0

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

Nikul Rao
  • 83
  • 8
-1

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" />
Marcin D
  • 918
  • 7
  • 14