Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect whether TextView in ListView is ellipsized

I have a custom Adapter that renders some items in a ListView. I need to show an icon on the ListView's items, if the item's text is ellipsized, and hide it if there's enough room for the text to finish. I have access to the button in getView method of my adapter (where I set the text) but the ellipses are not added immediately upon setting the text.

Is there any way I can do this?

Here's my TextView markup:

<TextView android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:ellipsize="end"
          android:singleLine="true"
          android:id="@+id/list_item_description"/>
like image 798
Hadi Eskandari Avatar asked May 02 '11 09:05

Hadi Eskandari


People also ask

How do I know if TextView Ellipsized?

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

What is Ellipsize in TextView?

Android Ellipsize Android TextView ellipsize property Causes words in the text that are longer than the view's width to be ellipsized ( means to shorten text using an ellipsis, i.e. three dots …) instead of broken in the middle to fit it inside the given view.

Can we Nest TextView inside TextView?

In Android all layout can be nested one another.

How do I center a TextView layout?

If in linearlayout your orientation vertical, you can put the textview in the "horizontal center" by android:layout_gravity="center" . For centering textview vertically you need to set layout_height of textview to match_parent and set android:gravity to "center".


1 Answers

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 :

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

Since the getLayout() cant be called before the layout is set, use ViewTreeObserver to find when the textview is loaded:

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.

like image 60
amalBit Avatar answered Sep 28 '22 10:09

amalBit