I want to change the ellipsis string from ...
to custom string, such as ...[more]
.
But in TextUtil, the ellipsis string is fixed:
private static final String ELLIPSIS_STRING = new String(ELLIPSIS_NORMAL);
Then how to change the ellipses?
I want to change the ellipsis string from ...
to custom string, such as ...[more]
.
But in TextUtil, the ellipsis string is fixed:
private static final String ELLIPSIS_STRING = new String(ELLIPSIS_NORMAL);
Then how to change the ellipses?
I was surprised that there isn't any good solution to that yet, so I spent sometime and wrote my own custom TextView which allows you to show a custom ellipsis. Few things I had in mind while developing it:
Have a look and let me know if you find any issues: https://github.com/TheCodeYard/EllipsizedTextView
And here's a screenshot:
I found the answer in How to use custom ellipsis in Android TextView. But you must run the function after layout, you can run it on OnGlobalLayoutListener.onGlobalLayout() or view.post().
Try This Method
public void CustomEllipsize(final TextView tv, final int maxLine, final String ellipsizetext) {
if (tv.getTag() == null) {
tv.setTag(tv.getText());
}
ViewTreeObserver vto = tv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
ViewTreeObserver obs = tv.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
if ((maxLine+1) <= 0) {
int lineEndIndex = tv.getLayout().getLineEnd(0);
String text = tv.getText().subSequence(0, lineEndIndex - ellipsizetext.length()) + " " + ellipsizetext;
tv.setText(text);
} else if (tv.getLineCount() >= (maxLine+1)) {
int lineEndIndex = tv.getLayout().getLineEnd((maxLine+1) - 1);
String text = tv.getText().subSequence(0, lineEndIndex - ellipsizetext.length()) + " " + ellipsizetext;
tv.setText(text);
}
}
});
}
and use this method like this
final TextView txtView = (TextView) findViewById(R.id.txt);
String dummyText = "sdgsdafdsfsdfgdsfgdfgdfgsfgfdgfdgfdgfdgfdgdfgsfdgsfdgfdsdfsdfsdf";
txtView.setText(dummyText);
CustomEllipsize(txtView, 2, "...[more]");