MacaronLover MacaronLover - 3 months ago 17
Android Question

Text view text not wrapping properly

I'm trying to get my text to wrap underneath the top line on the left but it won't let me. I've even used

wrap_content
.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:screenOrientation="portrait"
android:gravity="center_horizontal">

<TextView
android:text="@string/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@android:style/TextAppearance.Large"
android:textColor="@color/wc"
android:layout_gravity="center_horizontal" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:text="@string/yellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
style="@android:style/TextAppearance.Medium"
android:textColor="@color/yellow" />
<TextView
android:text="@string/teal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
style="@android:style/TextAppearance.Medium"
android:textColor="@color/teal" />
<TextView
android:text="@string/red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
style="@android:style/TextAppearance.Medium"
android:textColor="@color/red" />
<TextView
android:text="@string/and"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
style="@android:style/TextAppearance.Medium"
android:textColor="@color/white" />
<TextView
android:text="@string/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
style="@android:style/TextAppearance.Medium"
android:textColor="@color/white" />
<TextView
android:text="@string/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
style="@android:style/TextAppearance.Medium"
android:textColor="@color/white" />
</LinearLayout>
</LinearLayout>

Answer

You Can't use several textviews for that because several TextViews in a row gets align one after another and you can't simple make the last one to go under if the text is to big. Use one TextView and by code write your text with the help of html to get the several colors. Like This:

TextView txt = (TextView)findViewById(R.id.my_text_view_id);
txt.setText(Html.fromHtml("<font color="yellow">Way out,</font><font color="blue"> DLR,</font><font color="red"> Central</font>< color="white">  and Northern</font>"));
Comments