1

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.

cloud
  • 505
  • 1
  • 8
  • 21
  • Use a relative layout and align your views, you can even use the GUI of the Editor. – Skynet Aug 18 '14 at 11:36
  • http://stackoverflow.com/questions/2248759/how-to-layout-text-to-flow-around-an-image Here you can see how to use flowtexthelper. The principle is the same, your textview will wrap around the first view – dzsonni Aug 18 '14 at 11:43

4 Answers4

3

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));
Ozan
  • 1,191
  • 2
  • 16
  • 31
  • ok, thanks a lot. this can fix my problem. but what if i need to bind a listener on textView1 in the future? – cloud Aug 18 '14 at 11:40
  • @cloud,then you implement link to word in TextView content. – Haresh Chhelana Aug 18 '14 at 11:42
  • @cloud good question, I think there is no part text listener for textview – Ozan Aug 18 '14 at 11:43
  • @Haresh i can't really understand what you said, would you please give me an detail answer below? thank you – cloud Aug 18 '14 at 11:46
  • @cloud now I think there is a way to link a word :http://stackoverflow.com/questions/9969789/clickable-word-inside-textview-in-android – Ozan Aug 18 '14 at 11:50
1

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

Community
  • 1
  • 1
MHP
  • 2,613
  • 1
  • 16
  • 26
0

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.

paradite
  • 6,238
  • 3
  • 40
  • 58
0
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>
Tushski
  • 242
  • 2
  • 12
  • thanks for your answer, but there is a problem that how can i know how long textview2 shuold be? because textview2 and textview3 actually are the same string,just different part. – cloud Aug 19 '14 at 02:48