21

I am wondering how to have a TextView display its content on several lines without hardcoding the width in the XML.

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="false"
            android:text="Long multiline text"/>

        <TextView
            android:textColor="@color/text_color"
            android:layout_width="130dp"
            android:layout_height="wrap_content"
            />

    </LinearLayout>

Any thought welcome.

EDIT: my problem is that when the text exceeds the width set (because it reaches the end of the screen) a portion of the text is just not displayed. I would expect the text to be split on two lines

Community
  • 1
  • 1
znat
  • 13,144
  • 17
  • 71
  • 106
  • 1
    I'm confused by what you want. When the TextView reaches the width of its parent, then a new lines starts. Other than that where _do_ you want the lines breaks? You can manually break the line with: `android:text="long \nmultiline \ntext"`. You can also use the `maxWidth` attribute to wrap the TextView around the content until a desired width. – Sam Mar 06 '13 at 21:38
  • You are right. I have edited my question with more details – znat Mar 06 '13 at 21:54
  • Isn't that the default behavior of a `TextView`? What happens if you remove `maxLines` and `singleLine`? – nhaarman Mar 06 '13 at 21:57
  • If I set the layout_width to a fixed value, it works. But I can't do that as the TextView has to fill the available space – znat Mar 06 '13 at 22:01
  • Is this in a HorizontalScrollView? – Sam Mar 06 '13 at 22:12
  • It's in a LinearLayout. I have added it to the question. – znat Mar 06 '13 at 22:20
  • Do you really need the second `TextView` with the fixed width? – nhaarman Mar 06 '13 at 22:23
  • If I copy paste your xml file, the text gets wrapped as expected (only increasing the length of the text), it only is moved a bit off the screen. I will post a possible solution. – nhaarman Mar 06 '13 at 22:48

4 Answers4

24

Though I cannot reproduce the not wrapping problem, you can fix the positioning problem by using a weight on the first TextView. Using the following XML gives the expected output in the graphical layout view in Eclipse:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:singleLine="false"
        android:text="Long multiline text"/>

    <TextView
        android:textColor="@color/text_color"
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        />

</LinearLayout>
nhaarman
  • 98,571
  • 55
  • 246
  • 278
3

Also add

android:minLines="2"
android:scrollHorizontally="false"
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Wafaa BEK
  • 76
  • 3
0

You could try

android:inputType="textMultiLine"

in your TextView XML. This worked for me.

remykarem
  • 2,251
  • 22
  • 28
  • 5
    This is incorrect - android:inputType should only be used for EditText - use singleLine = false for textViews – RMcGuigan Jun 10 '18 at 15:12
  • @RMcGuigan, TextView also has an XML attribute called android:inputType. Please see https://developer.android.com/reference/android/widget/TextView. – remykarem Jun 11 '18 at 03:11
  • It does yes, but it should not be used for setting textview to multi-line - as per lint warning. See my original comment for the correct solution – RMcGuigan Jun 11 '18 at 11:13
0

I think I had very similar problem. I had a TextView with a text, where I was not sure how much lines will it take. It was encapsulated by a LinearLayout having android:layout_width="match_parent" to ensure my text will fill out all the space horizontally. However, the problem was that my text did not fit into 1 line and when it did break into a new line, the next view component below it did not move downwards to give enough space for the second line to be viewable fully.

I could achieve the solution by changing the LinearLayout that was containing my TextView into a RelativeLayout. By this way, the element below the text (actually below the Layout itself) was moved automatically to give enough space for the multi-line text.

Joseph
  • 51
  • 3