Proper Use for TextUtils.commaEllipsize

I am looking for sample code for proper use with TextView .

The only thing I found in my Google search was a test block for the TextUtils class .

Some recommendations would be highly appreciated.

EDIT:

I looked at the answer I received and tried to implement it in my code. I used this piece of code:

  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 was supposed to be.

it was more like: Mo, Joe, Isaac, Beta ...

instead: Mo, Joe, Isaac + 3

+4
source share
1 answer
 public static CharSequence commaEllipsize (CharSequence text, TextPaint p, float avail, String oneMore, String more) 

Options:

text - text to truncate p - Paint, with which you can measure the text
avail - horizontal width for text
oneMore - string for "1 more" in the current locale

more - a string for "% d more" in the current locale

Usage example:

 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); 
+4
source

Source: https://habr.com/ru/post/1447805/


All Articles