How do I know if my text was an ellipsis?

I have a multi-line TextView that has android:ellipsize="end" . I would like to know, however, if the line I am posting there is actually too long (so that I can make sure that the full line is shown elsewhere on the page).

I could use TextView.length() and find what the approximate length of the string will match, but since it is multiple lines, the TextView handles when to wrap, so this will not always work.

Any ideas?

+69
android textview ellipsis
Oct 23 '10 at 20:31
source share
11 answers

You can get a TextView layout and check the number of ellipses per line. For a finite ellipse, just check the last line, for example:

 Layout l = textview.getLayout(); if (l != null) { int lines = l.getLineCount(); if (lines > 0) if (l.getEllipsisCount(lines-1) > 0) Log.d(TAG, "Text is ellipsized"); } 

This only works after the layout phase, otherwise the returned layout will be null, so call it in the appropriate place in your code.

+114
Oct 24 2018-10-24
source share
β€” -

textView.getLayout is the way to go, but the problem is that it returns null if the layout is not prepared. Use the solution below.

  ViewTreeObserver vto = textview.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { Layout l = textview.getLayout(); if ( l != null){ int lines = l.getLineCount(); if ( lines > 0) if ( l.getEllipsisCount(lines-1) > 0) Log.d(TAG, "Text is ellipsized"); } } }); 
+31
Oct 08 '13 at 7:20
source share

I think the simplest solution to this question is the following code:

 String text = "some looooong text"; textView.setText(text); boolean isEllipsize = !((textView.getLayout().getText().toString()).equalsIgnoreCase(text)); 

This code assumes a maxLineCount installed in your XML TextView :)

+20
Nov 06 '15 at 11:47
source share

public int getEllipsisCount (int line) :

Returns the number of characters to ellipse, or 0 if there should not be an ellipsis.

So just call:

 int lineCount = textview1.getLineCount(); if(textview1.getLayout().getEllipsisCount(lineCount) > 0) { // Do anything here.. } 

Since getLayout () cannot be called before setting the layout, use this:

 ViewTreeObserver vto = textview.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { Layout l = textview.getLayout(); if ( l != null){ int lines = l.getLineCount(); if ( lines > 0) if ( l.getEllipsisCount(lines-1) > 0) Log.d(TAG, "Text is ellipsized"); } } }); 

And finally, don't forget to remove removeOnGlobalLayoutListener when you need it.

+4
Feb 21 '14 at 7:32
source share

This worked for me:

 textView.post(new Runnable() { @Override public void run() { if (textView.getLineCount() > 1) { //do something } } }); 
+4
Jul 27 '18 at 17:05
source share
 lateinit var toggleMoreButton: Runnable toggleMoreButton = Runnable { if(reviewTextView.layout == null) { reviewTextView.post(toggleMoreButton) return@Runnable } readMoreButton.visibility = if(reviewTextView.layout.text.toString() != comment) View.VISIBLE else View.GONE } reviewTextView.post(toggleMoreButton) 
+3
Dec 22 '18 at 5:32
source share

I created a custom TextView with a listener notification for this very problem. It also fixes the multi-line ellipse problem. You can find it in multi-line text view of android ellipse

+2
Jul 20 2018-11-11T00:
source share

he works for me

 if (l != null) { int lines = l.getLineCount(); if (lines > 0) { for (int i = 0; i < lines; i++) { if (l.getEllipsisCount(i) > 0) { ellipsize = true; break; } } } } 
+1
Aug 29 '16 at 3:58
source share

They really work like this, for example, to pass the full data to the dialog from the RecyclerView element:

 holder.subInfo.post(new Runnable() { @Override public void run() { Layout l = holder.subInfo.getLayout(); if (l != null) { final int count = l.getLineCount(); if (count >= 3) { holder.subInfo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { final int c = holder.subInfo.getLineCount(); if (c >= 3) { onClickToShowInfoDialog.showDialog(holder.title.getText().toString(), holder.subInfo.getText().toString()); } } }); } } } }); 
0
May 13 '18 at 16:38
source share

Using getEllipsisCount will not work with text that has blank lines. I used the following code to make it work:

 message.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw() { if(m.isEllipsized == -1) { Layout l = message.getLayout(); if (message.getLineCount() > 5) { m.isEllipsized = 1; message.setMaxLines(5); return false; } else { m.isEllipsized = 0; } } return true; } }); 

Remember to set maxLineCount in your XML. Then you can check the lineCount in your code, and if it is more than a certain number, you can return false to cancel the TextView drawing, and set the number of lines, as well as a flag to keep the text looking too long or not. The text view will again be drawn with the correct number of lines, and you will find out if its ellipsis or not with the flag.

Then you can use the isEllipsized flag to perform all necessary actions.

-one
Feb 14 '15 at 17:18
source share

create a method inside the TextViewUtils class

 public static boolean isEllipsized(String newValue, String oldValue) { return !((newValue).equals(oldValue)); } 

calling this method when required, for example:

  if (TextViewUtils.isEllipsized(textviewDescription.getLayout().getText().toString(), yourModelObject.getDescription())) holder.viewMore.setVisibility(View.VISIBLE);//show view more option else holder.viewMore.setVisibility(View.GONE);//hide 

but textView.getLayout () cannot call before setting the view (layout).

-one
Mar 28 '17 at 9:15
source share



All Articles