4

I'm looking for some sample code on how to use this correctly with a TextView.

the only thing I found in my Google searching was this test unit for the TextUtils class.

some guidance will be much appreciated.

EDIT:

I looked over the answer I got here and tried to implement it on my code. I used this code snippet:

    TextView title = (TextView) view.findViewById(R.id.listitemThreadsTitle);
    title.setVisibility(View.VISIBLE);

    TextPaint p = title.getPaint();
    String strTitle = "Moe, Joe, Isaac, Bethany, Cornelius, Charlie";
    title.setText(strTitle);
    float avail = p.measureText(strTitle);
    CharSequence ch = TextUtils.commaEllipsize(strTitle, p, avail, "one more", "%d more");
    title.setText(ch);

but the result was absolutely not what it's supposed to be.

it was more like: Moe, Joe, Isaac, Betha...

instead of: Moe, Joe, Isaac + 3

thepoosh
  • 12,497
  • 15
  • 73
  • 132

1 Answers1

4
public static CharSequence commaEllipsize (CharSequence text, TextPaint p, 
                                       float avail, String oneMore, String more)

Parameters:

text - the text to truncate
p - the Paint with which to measure the text
avail - the horizontal width available for the text
oneMore - the string for "1 more" in the current locale
more - the string for "%d more" in the current locale

Example Usage:

String text = "Apple, Orange, Mango, Banana";
TextView tv = new TextView(context);
float textWidth = tv.getPaint().measureText(text );
String tempStr = TextUtils.commaEllipsize(text, tv.getPaint(), textWidth, 
                                           "1 more", "%d more");
tv.setText(tempStr);

Update:

TextView title = (TextView) view.findViewById(R.id.listitemThreadsTitle);
title.setVisibility(View.VISIBLE);

TextPaint p = title.getPaint();
String strTitle = "Moe, Joe, Isaac, Bethany, Cornelius, Charlie";
title.setText(strTitle);
float avail = title.getMeasuredWidth();
CharSequence ch = TextUtils.commaEllipsize(strTitle, p, avail, "one more", "%d more");
title.setText(ch);
Ron
  • 24,175
  • 8
  • 56
  • 97
  • well, how do I check the textwidth? I have the the TextView tv. should I use `tv.getPaint(). measureText(text)`? – thepoosh Nov 22 '12 at 19:04
  • well, I tried using `tv.getPaint().measureText(text)`, but this is not working properly, I get a 0 result from this, meaning that there is no Elipsis happening. how can I measure the available size I have for the text? – thepoosh Nov 26 '12 at 09:31
  • Use `title.getMeasuredWidth()` for getting the avail value.. Assuming the `title` width is set to match_parent.. – Ron Nov 26 '12 at 14:55
  • this didn't work :( ended up writing a primitive function to do this myself – thepoosh Nov 27 '12 at 06:50
  • then get the title's parent width into avail.. `title.getParent().getWidth()` or `title.getParent().getMeasuredWidth()` – Ron Nov 27 '12 at 07:31
  • `avail = parent width - icon width`.. avail is the max width available for the textview in its parent.. – Ron Nov 27 '12 at 16:23