3

I have ListView row implemented as LinearLayout. There are 3 TextViews inside oriented horizontally and aligned to the left. Normally a row looks like this:
0 Normal Nickname 18.12.2013
But sometimes the nickname text is very long, so I want it to be ellipsized when all three TextViews don't feet on the screen. I want text to look like this:
0 Ridiculously Long Ni... 18.12.2013
but I get this instead:
0 Ridiculously Long Nickname 18.12.2

Layout

<TextView
        android:id="@+id/cmnt_row_rating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
/>

<TextView
        android:id="@+id/cmnt_row_author"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/cmnt_row_rating"
        android:gravity="left"
        android:lines="1"
        android:ellipsize="end"
        android:scrollHorizontally="true"
/>

<TextView
        android:id="@+id/cmnt_row_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/cmnt_row_author"
        android:gravity="left"
        android:lines="1"
/>

Is there a way to achieve effect I need?

Sergi0
  • 1,084
  • 14
  • 28

3 Answers3

2

Try this.. Use android:singleLine="true" for that TextView and also equally space for each TextView use android:layout_weight="1" .For weight you need to use LinearLayout

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

<TextView
        android:id="@+id/cmnt_row_rating"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true"
        android:gravity="left"
/>

<TextView
        android:id="@+id/cmnt_row_author"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="left"
        android:lines="1"
        android:singleLine="true"
        android:ellipsize="end"
        android:scrollHorizontally="true"
/>

<TextView
        android:id="@+id/cmnt_row_date"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true"
        android:gravity="left"
        android:lines="1"
/>
</LinearLayout>
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • well, this do the trick with ellipses, but makes all textviews the same width and I don't need that – Sergi0 Dec 18 '13 at 06:13
  • @Sergi0 then use `android:layout_weight="1"` for only one `TextView` for other remove it and use `android:layout_width="wrap_content"` – Hariharan Dec 18 '13 at 06:17
  • ok, this one is better, but it gets date to be aligned to the right, cause nickname takes all the free space. – Sergi0 Dec 18 '13 at 06:34
  • ok, accepting yours, cause it closer then others. can't accept ems version cause rows have different width. – Sergi0 Dec 18 '13 at 21:45
0

Set the android:ems="" like this:

<TextView
        android:id="@+id/cmnt_row_rating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="left"
/>

<TextView
        android:id="@+id/cmnt_row_author"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/cmnt_row_rating"
        android:gravity="left"
        android:lines="1"
        android:ellipsize="end"
        android:ems="3"
        android:scrollHorizontally="true"
/>

<TextView
        android:id="@+id/cmnt_row_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/cmnt_row_author"
        android:gravity="left"
        android:lines="1"
/>

I have set it 3. Change it according to your need.

Avijit
  • 3,834
  • 4
  • 33
  • 45
  • this makes middle part always take the same space, no matter it 3 or 10 characters long: `0 Short_____________18.12.2013` instead of `0 Short_18.12.2013` – Sergi0 Dec 18 '13 at 06:54
0

use android:maxEms attribute to limit characters in your TextView. change maxEms value according to your need.

<TextView
    android:id="@+id/cmnt_row_author"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/cmnt_row_rating"
    android:gravity="left"
    android:lines="1"
    android:ellipsize="end"
    android:maxEms="4"
    android:scrollHorizontally="true"
/>
Blo
  • 11,903
  • 5
  • 45
  • 99
rachit
  • 1,981
  • 15
  • 23
  • the problem is that text looks like this `0 Ridiculously L...______18.12.2013`instead looking like this `0 Ridiculously L..._18.12.2013`. So it truncates the middle view, but it still takes all the space. – Sergi0 Dec 18 '13 at 06:42