0

How can I make a multiline TextView that truncates the text properly? My TextView is part of a TableRow, so its width is a weight. The following does not work. The result is simply truncated text without the ellipses. I'm using API 10 (Android 2.3.3).

<TextView
        android:id="@+id/name_recipe"
        android:layout_centerHorizontal="true"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="8"
        android:text="Fried pork with mushrooms and potatoes and cilantro"
        android:ellipsize="end"
        android:maxLines="2"
        android:paddingLeft="10dp" />

Reducing the two lines to a single line with the following does work, but it's not ideal:

android:singleLine="true"

I don't like this for two reasons:

  1. android:singleLine is deprecated
  2. I'd much prefer to have the text be on two lines than one.

I've seen discussion about this like from here but I'm hoping a new solution has been found by now.

Community
  • 1
  • 1
Tianxiang Xiong
  • 3,887
  • 9
  • 44
  • 63

2 Answers2

3

Try this, it's from my project and works on phone but unfortunately not on tablet emulator:

<TextView
        android:id="@+id/myItem"
        android:layout_gravity="bottom|center_vertical"
        android:textSize="10sp"
        android:maxLines="2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:ellipsize="end"
        android:lines="2" />
ania
  • 2,352
  • 1
  • 20
  • 20
0

Use lines="1" as all the other methods are deprecated. As well as set scrollHorizontally.

<TextView 
        android:id="@+id/myid" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"        
        android:lines="1"       
        android:scrollHorizontally="true"
        android:ellipsize="end"
        android:layout_weight="1"
/>
Pete
  • 3,842
  • 3
  • 31
  • 42
  • Hi Pete, unfortunately this does not work either. The ellipses simply don't show up. Thanks for the reply though. – Tianxiang Xiong Aug 12 '11 at 07:19
  • Pete, shouldn't `scrollHorizontally` be `false` if you want to do `android:ellipsize="end"`. (It should be `true` if you want to do `android:ellipsize="marquee"`.) – Adil Hussain Aug 01 '12 at 13:19