Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 921
SK9 Avatar asked Mar 08 '11 08:03

SK9


People also ask

How do you add multiple lines in TextView?

When you have a text that's longer than one line, then the TextView will automatically put the text in multiple lines. When you set the layout_width and layout_height as wrap_content or match_parent , then the TextView widget will use all the available space to display the text you specified as its content.

Can you have multiple styles inside TextView?

Use multiple widgets if you need precise placement of discrete bits of text. Use inline markup if you, um, need markup inline in a widget. Remember: there is no FlowLayout in Android, so stringing together multiple TextViews to create a paragraph is not truly practical AFAIK.

How do I add a newline to a TextView in android?

for the new line in TextView just add \n in middle of your text it works..


2 Answers

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> 
like image 148
Michael Biermann Avatar answered Oct 05 '22 11:10

Michael Biermann


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.

like image 40
SK9 Avatar answered Oct 05 '22 11:10

SK9