CodeMonkey
CodeMonkey

Reputation: 12444

Forcing a TextView to be with a multiple line text

This is a follow up question to this question:

Force TextView to mutiline without \n

I got a TextView which is declared like this:

<TextView
        android:id="@+id/myTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.4"
        android:gravity="left|start"
        android:text="TextView"
        android:singleLine="false"/>

In the code I set a long text, and the text view display it as a long single line instead of breaking it to multiple lines. The accepted answer in the linked question suggests setting the android:maxWidth attribute, but I don't want to do it like that. I want the text lines to automatically break if the length of the text exceeds the 0.4 weight as set in the declaration of the text view. Is there a way of doing that and not using a constant size?

As requested this is the text view with its parent:

<LinearLayout
    android:id="@+id/linearLayoutBottomData"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/linearLayoutTopData"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="10dp"
    android:orientation="horizontal"
    android:weightSum="1">

    <TextView
        android:id="@+id/myTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.4"
        android:gravity="left|start"
        android:text="TextView"
        android:textColor="@android:color/black"
        android:textSize="@dimen/large_font_size"
        android:singleLine="false"/>

    <TextView
        android:id="@+id/myTextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.4"
        android:gravity="left|start"
        android:text="TextView"
        android:textColor="@android:color/black"
        android:textSize="@dimen/large_font_size"/>
</LinearLayout>

Upvotes: 3

Views: 10135

Answers (1)

SpiritCrusher
SpiritCrusher

Reputation: 21073

Use the below layout I have mentioned in comments too.

<LinearLayout
android:id="@+id/linearLayoutBottomData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayoutTopData"
android:layout_marginBottom="5dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="1">

<TextView
    android:id="@+id/myTextView1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.4"
    android:gravity="left|start"
    android:text="TextView"
    android:textColor="@android:color/black"
    android:textSize="@dimen/large_font_size"
    android:singleLine="false"/>

<TextView
    android:id="@+id/myTextView2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.4"
    android:gravity="left|start"
    android:text="TextView"
    android:textColor="@android:color/black"
    android:textSize="@dimen/large_font_size"/>

Upvotes: 4

Related Questions