cloud
cloud

Reputation: 527

multi-line textview,how to implement this layout

i am working with an android app. i have two textviews, textView1 is used to show username, and textView2 is used to show some words which are more than one line.

the layout looks like this:

enter image description here

the second line of textView2 should align with textView1 how to implements this layout? any idea would be appreciated.

Edit 1: actually, i need a solution which meets my need, you can use more than two textviews if needed.

Edit 2: Ozi's solution does fix my problem, but what if i need to bind a listener on textView1 in the future? is there any other solutions? thanks.

Upvotes: 1

Views: 338

Answers (4)

MHP
MHP

Reputation: 2731

you can use html:

txt.setText(Html.fromHtml(setText("username", "some long text")));  

private String setText(String title, String message) {
    return "<span>" + title + ": </span>$nbsp &nbsp &nbsp" + message;
}  

also you can set align=justify.
see this link . I think this is answer for your question

Upvotes: 1

Tushski
Tushski

Reputation: 242

Try this layout:
<TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1" >

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:background="#cecece"
                android:text="TextView"
                android:textColor="#000000" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:background="#FF0000"
                android:text="TextView"
                android:textColor="#000000" />
        </TableRow>

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_span="2"
                android:background="#FF0000"
                android:text="TextView TextView TextView TextView"
                android:textColor="#000000" />
        </TableRow>
    </TableLayout>

Upvotes: 0

paradite
paradite

Reputation: 6436

I think you can simply achieve this by stacking the two textviews together(one on top of another), and fix the width of the black textview. Then you just need to add some empty space in front of the red textview whenever you need to set text for it.

Upvotes: 0

Ozan
Ozan

Reputation: 1201

You can use 1 text view for all text. And you should change the style of black part like that:

   String blackstring = "<font color='#000000'>black</font>";
   t.setText(Html.fromHtml(blackstring + maintext));

Upvotes: 3

Related Questions