Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if textview is ellipsized in android

I have TextView with width as wrap content. In this TextView I set text, but text is not of the same length every time. When text is very long I use single line true and ellipsize: end. But now I have a problem. I want to set Visibility of other layout but that depends on the length my text. If text is too long to fit in the screen I want to setVisible true, but when text is short and when I don't need ellipsize, I want to set visibility false. So I need to check status of my TextView. When its ellipsize I want to setVisible true, when its not setVisible false. How I can do that. This is what I got:

tvAle.post(new Runnable() {

        @Override
        public void run() {

            int lineCount    = tvAle.getLineCount();
            Paint paint =  new Paint();
            paint.setTextSize(tvAle.getTextSize());
            final float size = paint.measureText(tvAle.getText().toString());
            Log.v("a", ""+size+" "+tvAle.getWidth());
            if ((int)size > (tvAle.getWidth()+10)) {
                allergiesLayout.setVisibility(View.VISIBLE);
            }

            else
                allergiesLayout.setVisibility(View.GONE);

        }

but this solution doesn't work.

like image 632
user1302569 Avatar asked Mar 22 '13 09:03

user1302569


People also ask

How to check TextView is ellipsized android?

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

Which of the following is properties of TextView?

Properties of TextView in Android. android:id-ID is an attribute used to define a TextView uniquely. android: layout_height- Height is the attribute which is used to set the height of the TextView.

What is text view?

A TextView displays text to the user and optionally allows them to edit it. A TextView is a complete text editor, however the basic class is configured to not allow editing.


2 Answers

You can use this method provided: getEllipsisCount

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

where line could be obtained via getLineCount()

like image 127
Calvin Avatar answered Oct 22 '22 18:10

Calvin


Well, the accepted solution does work but misses a few corner cases because it will only check the last line for ellipsized characters. If we have a TextView consisting of two lines and use TruncateAt.START to truncate the text at its beginning, the accepted answer will fail. :-/

Adding an ViewTreeObserver.OnPreDrawListener seems more like a really expensive overhead to me. So I made the following improvements to the code of the accepted answer:

/**
 * Checks if the text of the supplied {@link TextView} has been ellipsized.
 *
 * @param textView
 *         The {@link TextView} to check its text.
 *
 * @return {@code True} if the text of the supplied {@code textView} has been ellipsized.
 */
public static boolean isTextViewEllipsized(final TextView textView) {
    // Check if the supplied TextView is not null
    if (textView == null) {
        return false;
    }

    // Check if ellipsizing the text is enabled
    final TextUtils.TruncateAt truncateAt = textView.getEllipsize();
    if (truncateAt == null || TextUtils.TruncateAt.MARQUEE.equals(truncateAt)) {
        return false;
    }

    // Retrieve the layout in which the text is rendered
    final Layout layout = textView.getLayout();
    if (layout == null) {
        return false;
    }

    // Iterate all lines to search for ellipsized text
    for (int line = 0; line < layout.getLineCount(); ++line) {

        // Check if characters have been ellipsized away within this line of text
        if (layout.getEllipsisCount(line) > 0) {
            return true;
        }
    }

    return false;
}

There is still room for improvement, though. But this method does suffice my use cases. Corrections and improvements are appreciated. :-)

like image 15
Sam del Rö Avatar answered Oct 22 '22 17:10

Sam del Rö