Android: Something better than android:ellipsize="end" to add "..." to truncated long Strings?

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.


Solution 1:

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.

Solution 2:

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.

Solution 3:

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)

Solution 4:

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