Android and displaying multi-lined text in a TextView in a TableRow

AndroidTextviewMultilineTablelayoutTablerow

Android Problem Overview


I'm displaying a TableLayout with rows as follows:

<?xml version="1.0" encoding="utf-8"?>
<TableRow
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">
  
  <RelativeLayout
  	android:layout_width="match_parent"
  	android:layout_height="wrap_content">
  
	  	<TextView	
	  		android:layout_width="match_parent"
	  		android:layout_height="wrap_content"
	  		android:id="@+id/one"
	  		android:layout_marginLeft="10dip"
	  		android:textColor="#B0171F" />
  		<TextView
  			android:layout_width="match_parent"
  			android:layout_height="wrap_content"
  			android:layout_below="@id/one"
  			android:id="@+id/two"
  			android:layout_marginLeft="10dip"
  			android:ellipsize="none"
	        android:singleLine="false"
  			android:scrollHorizontally="false"
  			android:maxLines="10"
  			android:textColor="@android:color/black" />
  
  </RelativeLayout>

</TableRow>

I'm hitting this with everything I can find here and can think of to permit the text to wrap on many lines but to no avail: The text is always forced to a single line, running off the screen. It might matter that I'm working inside a TableRow here, and so far as I can tell this hasn't been treated on this site.

So, how do I force my second TextView to wrap to many lines?

Android Solutions


Solution 1 - Android

The TextView will wrap the text if the column it's in is set to shrink. Sometimes it does not wrap exactly, if the text ends with ", ...", then it is a little bit longer than exactly n lines.

Here's an example, the TextView with the id question will wrap:

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:shrinkColumns="*">
    <TableRow>
         <TextView
             android:id="@+id/question"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"/>
    </TableRow>
</TableLayout>

Solution 2 - Android

For the sake of completeness, this is how my TableLayout reads in XML:

<TableLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="10dp"
  android:shrinkColumns="0"
  android:stretchColumns="0"
  android:id="@+id/summaryTable">
</TableLayout>

I later populate this with TableRows.

Solution 3 - Android

By code also works:

TableLayout tablelayout = (TableLayout) view.findViewById(R.id.table);
tablelayout.setColumnShrinkable(1,true);

1 is the number of column.

Solution 4 - Android

Maybe try as follows:

myTextView.setText(Html.fromHtml("On " + stringData1 + "<br> at " + strinData2);

I know, looks a bit like a "wheelchair" solution, but works for me, though I also fill a text programmatically.

Solution 5 - Android

You can also used below code

<TableRow >
    <TextView
        android:id="@+id/tv_description_heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:padding="8dp"
        android:text="@string/rating_review"
        android:textColor="@color/black"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/tv_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:maxLines="4"
        android:padding="8dp"
        android:text="The food test was very good."
        android:textColor="@color/black"
        android:textColorHint="@color/hint_text_color" />
</TableRow>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSK9View Question on Stackoverflow
Solution 1 - AndroidMichael BiermannView Answer on Stackoverflow
Solution 2 - AndroidSK9View Answer on Stackoverflow
Solution 3 - AndroidSusana Mar FloresView Answer on Stackoverflow
Solution 4 - Android87elementView Answer on Stackoverflow
Solution 5 - AndroidJitendra NandiyaView Answer on Stackoverflow