Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiline TextView with width "wrap_content"

Tags:

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

like image 247
znat Avatar asked Mar 06 '13 21:03

znat


2 Answers

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>
like image 111
nhaarman Avatar answered Sep 17 '22 18:09

nhaarman


Also add

android:minLines="2"
android:scrollHorizontally="false"
like image 32
Wafaa BEK Avatar answered Sep 19 '22 18:09

Wafaa BEK