如何判断文本视图是否为椭圆形?

我有一个多线 TextViewandroid:ellipsize="end"集。我想知道,但是,如果我放在那里的字符串实际上太长(这样我可以确保完整的字符串显示在页面的其他地方)。

我可以使用 TextView.length()并找出字符串的大致长度,但是由于它是多行的,所以 TextView处理什么时候换行,所以这并不总是有效。

有什么想法吗?

40387 次浏览

You can get the layout of the TextView and check the ellipsis count per line. For an end ellipsis, it is sufficient to check the last line, like this:

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 this at an appropriate place in your code.

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

 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");
}
}
});

Code snippet for removing the listener (source):

mLayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
scrollToGridPos(getCenterPoint(), false);
mLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});

public int getEllipsisCount (int line):

Returns the number of characters to be ellipsized away, or 0 if no ellipsis is to take place.

So, simply call :

int lineCount = textview1.getLineCount();


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

Since the getLayout() cant be called before the layout is set, 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 do not forget to remove removeOnGlobalLayoutListener when you need it nomore.

Using getEllipsisCount won't work with text that has empty lines within it. 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;
}
});

Make sure not to set a maxLineCount in your XML. Then you can check for the lineCount in your code and if it is greater than a certain number, you can return false to cancel the drawing of the TextView and set the line count as well as a flag to save whether the text view is too long or not. The text view will draw again with the correct line count and you will know whether its ellipsized or not with the flag.

You can then use the isEllipsized flag to do whatever you require.

I think the easiest 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 that in your XML the TextView set a maxLineCount :)

it is working 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;
}
}
}
}

create a method inside your TextViewUtils class

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

call this method when it's required eg:

        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() can't call before the view(layout) set.

Really work so, for example, to pass full data to dialog from item of RecyclerView:

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());
}
}
});
}
}
}
});

This worked to me:

textView.post(new Runnable() {
@Override
public void run() {
if (textView.getLineCount() > 1) {
//do something
}
}
});
lateinit var toggleMoreButton: Runnable
toggleMoreButton = Runnable {
if(reviewTextView.layout == null) { // wait while layout become available
reviewTextView.post(toggleMoreButton)
return@Runnable
}
readMoreButton.visibility = if(reviewTextView.layout.text.toString() != comment) View.VISIBLE else View.GONE
}
reviewTextView.post(toggleMoreButton)

It is some typical case:

  1. comment in 'reviewTextView'
  2. comment can collapsed by some criteria
  3. if comment collapsed you show button 'readMoreButton'

The Kotlin way:

textView.post {
if (textView.lineCount > MAX_LINES_COLLAPSED) {
// text is not fully displayed
}
}

Actually View.post() is executed after the view has been rendered and will run the function provided

Combining @Thorstenvv awnser with @Tiano fix, here is the Kotlin version :

val layout = textView.layout ?: return@doOnLayout
val lines = layout.lineCount
val hasLine = lines > 0
val hasEllipsis = ((lines - 1) downTo 0).any { layout.getEllipsisCount(it) > 0 }
if (hasLine && hasEllipsis) {
// Text is ellipsized
}

In Kotlin, you can use the below code.

var str= "Kotlin is one of the best languages."
textView.text=str
textView.post {
val isEllipsize: Boolean = !textView.layout.text.toString().equals(str)


if (isEllipsize) {
holder.itemView.tv_viewMore.visibility = View.VISIBLE
} else {
holder.itemView.tv_viewMore.visibility = View.GONE
}
}

Simple Kotlin method. Allows android:ellipsize and android:maxLines to be used

fun isEllipsized(textView: TextView, text: String?) = textView.layout.text.toString() != text

The most eloquent solution I have found (in Kotlin) is to create an extension function on TextView

fun TextView.isEllipsized() = layout.text.toString() != text.toString()

This is great because it doesn't require knowing what the full string is or worrying about how many lines the TextView is using.

TextView.text is the full text that it's trying to show, whereas TextView.layout.text is what's actually shown on the screen so if they are different it must be getting ellipsized

To use it:

if (my_text_view.isEllipsized()) {
...
}

This is simple library for creating textview expandable. Like Continue or Less. This library extended version TextView. Easy to use.

implementation 'com.github.mahimrocky:ShowMoreText:1.0.2'

Like this, 1 https://raw.githubusercontent.com/mahimrocky/ShowMoreText/master/screenshot1.png

2 https://raw.githubusercontent.com/mahimrocky/ShowMoreText/master/screenshot2.png

 <com.skyhope.showmoretextview.ShowMoreTextView
android:id="@+id/text_view_show_more"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text"
/>

In Activity you can use like:

ShowMoreTextView textView = findViewById(R.id.text_view_show_more);


//You have to use following one of method


// For using character length
textView.setShowingChar(numberOfCharacter);
//number of line you want to short
textView.setShowingLine(numberOfLine);

If your textview contains multiple paragraphs, using getEllipsisCount will not work for empty lines within it. getEllipsisCount for the last line of any paragraph will return 0.

Solution with kotlin extensions:

infoText.afterLayoutConfiguration {
val hasEllipsize = infoText.hasEllipsize()
...
}

Extensions:

/**
* Function for detect when layout completely configure.
*/
fun View.afterLayoutConfiguration(func: () -> Unit) {
viewTreeObserver?.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
viewTreeObserver?.removeOnGlobalLayoutListener(this)
func()
}
})
}


fun TextView.hasEllipsize(): Boolean = layout.getEllipsisCount(lineCount - 1) > 0