28

I have a listView with custom objects defined by the xml-layout below. I want the textView with id "info" to be ellipsized on a single line, and I've tried using the attributes

android:singleLine="true"
android:ellipsize="end"

without success.

If I set the layout_width to a fixed width like e.g.

android:layout_width="100px"

the text is truncated fine. But for portability reasons this is not an acceptable solution.

Can you spot the problem?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5px"
>
<TextView  
android:id="@+id/destination"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:textSize="22dp"
android:paddingLeft="5px"
/>

<TextView  
android:id="@+id/date"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:textSize="15dp"
android:paddingLeft="5px"
/>

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/info_table"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="5px"
android:paddingTop="10px" 
>
    <TableRow>
        <TextView
            android:id="@+id/driver_label"
            android:gravity="right"
            android:paddingRight="5px"
            android:text="@string/driver_label" />
        <TextView
            android:id="@+id/driver" />
    </TableRow>
    <TableRow>
        <TextView
            android:id="@+id/passenger_label"
            android:gravity="right"
            android:paddingRight="5px"
            android:text="@string/passenger_label" />
        <TextView
            android:id="@+id/passengers" />
    </TableRow>
    <TableRow>
        <TextView
            android:id="@+id/info_label"
            android:gravity="right"
            android:paddingRight="5px" 
            android:text="@string/info_label"/>
        <TextView
            android:id="@+id/info"
            android:layout_width="fill_parent"
            android:singleLine="true"
            android:ellipsize="end" />
    </TableRow>
</TableLayout> 

aspartame
  • 4,622
  • 7
  • 36
  • 39

9 Answers9

40

Without using deprecated properties, the following lines work great for me

android:ellipsize="end" 
android:lines="1"
android:scrollHorizontally="true"

ref http://code.google.com/p/android/issues/detail?id=882#c9

tbruyelle
  • 12,895
  • 9
  • 60
  • 74
  • Even without lines, ellipsize seems to be working only with the scrollHorizontally setting. – erdomester Mar 15 '12 at 22:29
  • 6
    It will not work with Android 4.0. Use istead of code above: `android:ellipsize="end" android:SingleLine="true" android:scrollHorizontally="true"` – Subtle Fox Oct 05 '12 at 09:06
  • Android Studio says android:singleLine is deprecated, it still needs for android:ellipsize to work – Atul May 29 '17 at 15:07
24

Ellipsize is broken (go vote on the bug report, especially since they claim it's not reproducible) so you have to use a minor hack. Use:

android:inputType="text"
android:maxLines="1"

on anything you want to ellipsize. Also, don't use singleLine, it's been deprecated.

UPDATE:

On closer inspection, the problem you're having is that your table is extending off the right side of the screen. Changing your TableLayout definition to:

<TableLayout
    android:id="@+id/info_table"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="5px"
    android:paddingTop="10px"
    android:shrinkColumns="1">

should fix that problem, then do what I said above to ellipsize your TextView.

Jeremy Logan
  • 47,151
  • 38
  • 123
  • 143
  • 2
    Thanks, but unfortunately it still doesn't work. Could it have something to do with the tableLayout? – aspartame Sep 15 '09 at 08:35
  • NP... also, WRT the TableLayout definition: You only need to declare `xmlns:android` on the very first XML element. – Jeremy Logan Sep 17 '09 at 08:40
  • 1
    This work-around is not working for me. I have a ListView, each item in the list has multiple text views in a TableLayout. When I added inputType='text' to the text view that can grow long, I can no longer select items from the list. Plus, I still did not see the ellipses. I did vote for the bug but it is closed and not reproducible. Perhaps a new bug report can be created. Any other ideas? – John in MD Oct 23 '09 at 03:37
  • I agree, this did not work for me. TextView grew long, offset other parts of the horizontal linear layout it was in. Is this attribute dependent on some specific variable. I ended up setting android:maxLength="x" and that trimmed the text, without the elipsis – bgs Dec 19 '10 at 09:35
  • Since when is android:singleLine depreciated? http://developer.android.com/reference/android/widget/TextView.html#attr_android:singleLine – Samuel Apr 27 '11 at 15:28
  • @Samuel see http://developer.android.com/reference/android/R.attr.html#singleLine – Giulio Piancastelli Nov 30 '11 at 15:34
  • @GiulioPiancastelli The constant singleLine has been deprecated, the method TextView.setSingleLine() is still valid. – ilomambo Feb 22 '13 at 05:21
  • What I found is though Android Studio says `android:singleLine` is deprecated, it still needs for `android:ellipsize` to work – Atul May 29 '17 at 15:06
7

I had the same results as Steve:

android:ellipsize="marquee" android:scrollHorizontally="true" android:lines="1"

works perfectly

Andy Cochrane
  • 2,409
  • 1
  • 18
  • 16
4

try...

i.e.

android:ellipsize="marquee"
android:focusable="true" android:marqueeRepeatLimit="num|marquee_forever"
android:lines="1" android:focusableInTouchMode="true"
android:scrollHorizontally="true"

as marquee only works when view is selected or focused according to the comment#2 on http://code.google.com/p/android/issues/detail?id=5364

Peter Ajtai
  • 56,972
  • 13
  • 121
  • 140
hwii77
  • 96
  • 2
  • I personally found that `android:ellipsize="marquee" android:scrollHorizontally="true" android:lines="1" ` was all you needed. – Steve Pomeroy May 28 '10 at 21:41
1

After checking out these 3 issues(closed & re-opened) and previous posts, I think the key is to wrap up TextView with a outer LinearLayout(or any other layout you are using), and set the "layout_width" of outer layout width a little bit SMALLER than the text length contained in the TextView. Seems has to LIMIT the width to make the "android:ellipsize" behave properly.

Follow the tip in this issue, after adding all these attributes for hacking, I also set the layout_width of outer LinearLayout to 240dp for example:

<Linearlayout
  android:layout_width="240dp"
  android:layout_height="wrap_content"
  ...>

    <TextView
     ....
     android:singleLine="true"
     android:lines="1"
     android:maxLines="1"
     android:scrollHorizontally="true"
     android:ellipsize="end"
     android:text="this is a text which length should be longer than 240dp"
    />
</LinearLayout>

Than I can successfully make the end of TextView displaying "...", not very acceptable but it works! And I think it works whether used in regular TextView or a TextView in a List Adapter.

Here's my screenshot for the result!

hope it helps ^^

Ref of those 3 issues:

issue 882

issue 10554

issue 31229

Robert
  • 1,660
  • 22
  • 39
  • I have added the code that works for me in the issue 882 https://code.google.com/p/android/issues/detail?id=882 . Thanks for the link. – wendigo Aug 19 '14 at 09:00
1

If you put TextView inside a custom ViewGroup you created, make sure to measure it again if you changed the dimension after the measurement.

E.g., I calculated the new dimension in onLayout. Before you call child.layout, call

measureChild(child,
    View.MeasureSpec.makeMeasureSpec(childWidth, View.MeasureSpec.EXACTLY),
    View.MeasureSpec.makeMeasureSpec(childHeight, View.MeasureSpec.EXACTLY));
Helin Wang
  • 4,002
  • 1
  • 30
  • 34
0

I found somewhere this attributes

<TextView ...
android:singleLine="true"
android:ellipsize="marquee" />

it work fine for me (Android 1.5)

molokoloco
  • 4,504
  • 2
  • 33
  • 27
  • 3
    singleLine is deprecated in 1.5 – Jeremy Logan Sep 16 '09 at 20:45
  • I deleted my earlier comment... can't find it in the docs but in the XML layout viewer of Eclipse it says that android:singleLine is depreciated and to use multiLine instead. – rf43 Oct 16 '11 at 10:14
0

For me these attribute worked. I tested on API 8 and onwards

In XML

<TextView
 ....
 android:maxLines="1"
 android:scrollHorizontally="true"
 android:ellipsize="end"
 ....
/>

Programmatically

setMaxLines(1);
setEllipsize(TruncateAt.END);
setHorizontallyScrolling(true);
Zeeshan
  • 1,625
  • 17
  • 25
  • Version as in OS is 4.4.2. Component is a parent listview and the target TextView is a title of the item in the List . – sud007 Apr 13 '16 at 06:08
0

I had the same problem in recycler view card. I have resolved this by adding following code:

    <TextView
        android:ellipsize="end"
        android:maxLines="1"/>
Shri
  • 91
  • 4