Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 425
OneWorld Avatar asked Sep 22 '10 12:09

OneWorld


2 Answers

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.

like image 128
bakkay Avatar answered Oct 22 '22 09:10

bakkay


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.

like image 26
Maragues Avatar answered Oct 22 '22 07:10

Maragues