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

OneWorld picture OneWorld · Sep 22, 2010 · Viewed 42k times · Source

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.

Answer

bakkay picture bakkay · Jun 12, 2012

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.