46

This property makes

"short and very-long-word"

to

"short and"

. But I want to have smth. like

"short and very-lon..."

Right now I truncate the String in Java code. However, thats based on the number of characters and not the actual length of the link. So, the result isn't very nice.

String title;
    if(model.getOrganization().length() > 19) {
      title = model.getText().substring(0, 15).trim() + "…";
    } else {
      title = model.getText();
    }
    ((TextView) findViewById(R.id.TextViewTitle)).setText(title);

Update

Just noticed, this property actually adds "..." in a few cases. But not in all of them:

12345678901234567890 becomes "12345678901234..."

However,

"1234567890 1234567890" becomes "1234567890" and not "1234567890 123..."

Update 2

Now it really gets funky! I just set singleLine=true and removed maxLine (The bug appears with and without setting ellipsize attribute)...

alt text

This is a screenshot take from Motorola Milestone with android 2.1 update 1. Same happens on HTC Desire with the same android version

Update 3

Now I use android:ellipsize="marquee". That seems to be the only properly working setting. It's also only moving, when focused. I see it in many other apps also. I guess its common practise.

Community
  • 1
  • 1
OneWorld
  • 16,782
  • 18
  • 79
  • 130

4 Answers4

67

If it's for a single line try this:

android:ellipsize="end"
android:maxLines="1"
android:scrollHorizontally="true"
android:singleLine="true"

It worked for me.

bakkay
  • 738
  • 8
  • 16
26

I had a similar problem and setting this property to the TextView helped:

android:singleLine="true"

Also, I was using a RelativeLayout so I limited the space the TextView could take by setting a left margin to a button to the right of the TextView, so that the text cannot expand further and is forced to truncate its content.

Maybe it's not the solution to your problem, but it helped me in a similar situation.

Maragues
  • 35,146
  • 14
  • 91
  • 94
  • 2
    Didn't help in my case. By the way "singleLine" is deprecated. I don't have to set margins, it is already tuncating, but not very nice. – OneWorld Sep 22 '10 at 16:25
  • check this question: http://stackoverflow.com/questions/1698881/cant-get-ellipsis-to-work-on-android I know singleline is deprecated, read the comments in the bug page from the linked question and you'll see why I'm using it. – Maragues Sep 23 '10 at 07:21
  • 1
    ```android:singleLine="true"``` is the only way it works well for me, ```android:lines=1``` will work but the ellipsis to end will start quite early (and looks terrible). – Bundeeteddee Jan 02 '18 at 13:19
1

See my update 3. The workaround was using " marquee". I have seen many apps doing that at this time. Now, with new versions of Android this feature is most likely fixed and will work as expected. (my guess)

OneWorld
  • 16,782
  • 18
  • 79
  • 130
1

Maybe you could take a look at how the private Ellipsizer class used by the OS works and modify it for your app?

Edit: Here's a working link - http://androidxref.com/5.1.0_r1/xref/frameworks/base/core/java/android/text/Layout.java#1830

Josh
  • 9,960
  • 2
  • 30
  • 36