Get part of String which was not truncated by ellipsize
android
Solution
You should be able to get it by effectively computing the ellipsised string yourself, something like this:
String ellipsised = TextUtils.ellipsize(textView.getText(),
textView.getPaint(),
(float)textView.getWidth(),
TextUtils.TruncateAt.END).toString();
This may not be the best approach, but it should work. `ellipsize` method isn't documented very well, so I'm not sure what it expects as the available width parameter (float), but looking through some source code it seems that it's the width of the text view.
UPDATE: Just tested this code. It works perfectly when the ellipsised truncation is in one line. If you specify `maxLines` on your original text view, then some other hack is needed.
UPDATE 2: After some struggle, I managed to write a method to get exact ellipsised text from a multiline wrapped textview. It's a lot more complex, but it does produce correct results.
private String getEllipsisedText(TextView textView) {
String text = textView.getText().toString();
int lines = textView.getLineCount();
int width = textView.getWidth();
int len = text.length();
TextUtils.TruncateAt where = TextUtils.TruncateAt.END;
TextPaint paint = textView.getPaint();
StringBuffer result = new StringBuffer();
int spos = 0, cnt, tmp, hasLines = 0;
while(hasLines < lines - 1) {
cnt = paint.breakText(text, spos, len, true, width, null);
if(cnt >= len - spos) {
result.append(text.substring(spos));
break;
}
tmp = text.lastIndexOf('\n', spos + cnt - 1);
if(tmp >= 0 && tmp < spos + cnt) {
result.append(text.substring(spos, tmp + 1));
spos += tmp + 1;
}
else {
tmp = text.lastIndexOf(' ', spos + cnt - 1);
if(tmp >= spos) {
result.append(text.substring(spos, tmp + 1));
spos += tmp + 1;
}
else {
result.append(text.substring(spos, cnt));
spos += cnt;
}
}
hasLines++;
}
if(spos < len) {
result.append(TextUtils.ellipsize(text.subSequence(spos, len), paint, (float)width, where));
}
return result.toString();
}
Note that this code assume that the ellipses are at the end. It shouldn't be too difficult to modify it to cater for other `ellipsize` options.
Problem
How to get part of String which weren't truncated in multiline TextView. For instance if I have "This is some text which is quite long as you see" and TextView would show "This is some text which is..." then I want to get that second String into some variable. What is easiest way to do so?