51

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?

Ken
  • 30,811
  • 34
  • 116
  • 155
  • A TableRow is just that - a row. I don't think you're supposed to try and create multiple lines on a single row, that's kinda the point. What's the purpose of using TableRow there? – Codemonkey Mar 08 '11 at 09:37
  • did you try removing the `android:singleLine="false"` line? – Aman Alam Mar 08 '11 at 09:46
  • Just copied your code in Eclipse, and the text is wrapping correctly, as I can see in Graphical Layout. (I just added `android:text="(a long text) " `to have some text to display) – Adinia Mar 08 '11 at 09:46
  • @All: Thank you for your posts. If it helps, I'm creating a custom TableLayout to display a variable number of rows, all at runtime. @Adinia: I'm setting the text at runtime, that might be it. Can that be the problem? – Ken Mar 08 '11 at 10:16
  • @All, esp. Adinia: Just tried setting a very long text in the XML, rather than programmatically, and it's not wrapping - still on one line. Hmm. Different simulator? No that can't be it. – Ken Mar 08 '11 at 10:22
  • @Sheikh Aman: I added that line after posting the question, I've tried with and without. – Ken Mar 08 '11 at 10:53
  • As @Adinia said, he/she tried your project in eclipse and it worked in the graphical layout editor. I don't think it would matter if you set a text dynamically or statically, since the `TextView`will take whatever property it is set to. can you post the screenshot of what's happening? what version of dev tools are you using!? – Aman Alam Mar 08 '11 at 16:01
  • @All: Complete answer given here, see @Michael's answer and the comments after. It can be done quite easily (when you know). – Ken Mar 23 '11 at 04:04

5 Answers5

99

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>
Michael Biermann
  • 3,105
  • 27
  • 23
  • Thanks for this. I'll try this out in due course (I have a few things to take care of on this project) and hope to accept an answer. – Ken Mar 22 '11 at 04:58
  • 12
    @All: `android:shrinkColumns="0"` works perfectly for me, rather than `android:shrinkColumns="1"`which I had been trying. I'm not sure if it's what you're suggesting, but your answer is solid and my comment appears here. Together they answered my question. – Ken Mar 23 '11 at 03:44
  • @All: android:stretchColumns="0" is also useful/better, it seems, if (as in my case) the table only has one column which you want to stretch to fill the screen. My TableLayout XML is given in my answer on this page (below at the time of typing). – Ken Mar 23 '11 at 12:31
  • Yes you are absolutely right about the confusion about stretch/shrinkColumns="???". Most of the time I use android:shrinkColumns="*". For some very special cases I explicitly use the column number I want to be stretched/shrunken. – Michael Biermann Jul 25 '11 at 23:04
  • 1
    Idk why but the android:stretchColumns didn't work for me. But the android:shrinkColumns works like a charm. Thank you for the answer & the supporting comments. – Ahmed Faisal Oct 24 '11 at 17:12
  • 2
    @SK9 The answer is correct, but a little sloppy. The purpose here is to make the column with the text shrink, so the column's index should be set in that parameter. I've updated the answer, hope it's become clearer now. – Malcolm Oct 02 '13 at 18:51
15

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.

Ken
  • 30,811
  • 34
  • 116
  • 155
3

By code also works:

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

1 is the number of column.

Susana Mar Flores
  • 2,478
  • 1
  • 18
  • 16
2

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.

87element
  • 1,909
  • 3
  • 19
  • 29
0

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>