12

How do I get the text that has been truncated by Android into an ellipsis?

I have a textview:

<TextView
    android:layout_width="120dp"
    android:layout_height="wrap_content"
    android:ellipsize="end"
    android:singleLine="true"
    android:text="Um longo texto aqui de exemplo" />

On a device this TextView is shown like this:

"Um longo texto a..."

How do I get the rest of the text?

I am looking for something like getRestOfTruncate() which would return "qui de exemplo".

Austyn Mahoney
  • 11,398
  • 8
  • 64
  • 85
ademar111190
  • 14,215
  • 14
  • 85
  • 114
  • I fixed your question's title and text. I wish I had an answer for you, but I don't think there is a way to do this. What is the use case here? – Austyn Mahoney Jun 14 '12 at 22:11
  • Thanks @AustynMahoney, should probably be something done to make it, else, I will build something, the problem is that it will be much more difficult, but if I do post here – ademar111190 Jun 14 '12 at 22:37
  • 1
    Use `android:text="@string/full_text"` in xml layout, and `getString(R.string.full_text)` in java code whenever you need it. – yorkw Jun 15 '12 at 03:11

3 Answers3

10
String text = (String) textView.getText().subSequence(textView.getLayout().getEllipsisStart(0), textView.getText().length());
Sudar Nimalan
  • 3,962
  • 2
  • 20
  • 28
  • Work perfect but i need insert your code inside a postRunnable because first the textview must be designed, in finishi i make this: new Handler().postDelayed(runnable with your code,1 milissegundo); – ademar111190 Jun 15 '12 at 15:35
  • 7
    This only works if I have `android:singleLine="true"`. If I set it to `false` and set `android:maxLines` to some number other than `, then this method always returns the entire text as if it were not ellipsised at all. – Aleks G Aug 22 '13 at 15:01
1

Using textView.getLayout().getEllipsisStart(0) only works if android:singleLine="true"

Here is a solution that will work if android:maxLines is set:

public static String getEllipsisText(TextView textView) {
    // test that we have a textview and it has text
    if (textView==null || TextUtils.isEmpty(textView.getText())) return null;
    Layout l = textView.getLayout();
    if (l!=null) {
        // find the last visible position
        int end = l.getLineEnd(textView.getMaxLines()-1);
        // get only the text after that position
        return textView.getText().toString().substring(end);
    }

    return null;
}

Remember: this works after the view is already visible.

Usage:

textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            Log.i("test" ,"EllipsisText="+getEllipsisText(textView));
        }
    });
Gil SH
  • 3,789
  • 1
  • 27
  • 25
1

My solution. Kotlin extension function:

fun TextView.getEllipsizedText(): String {
if (text.isNullOrEmpty()) return ""
return layout?.let {
    val end = textContent.text.length - textContent.layout.getEllipsisCount(maxLines - 1)
    return text.toString().substring(0, end)
} ?: ""
}
V. Gerasimenko
  • 209
  • 2
  • 10