Detect whether TextView in ListView is ellipsized

浪尽此生 提交于 2019-12-03 11:45:16

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.

The tricky part is that the view you're working with in getView will sometimes have been laid out, and sometimes not, so you have to handle both cases.

When it hasn't been laid out, you set a view tree observer to check on the ellipsis once it has been. In the case of recycled views, the layout will already be there and you can check for the ellipsis immediately after setting the text.

public View getView(int position, View convertView, ViewGroup parent) {
  final ViewHolder vh;
  if (convertView == null) {
    vh = new ViewHolder();
    ... // create/inflate view and populate the ViewHolder
  }
  vh = (ViewHolder) convertView.getTag();

  // Set the actual content of the TextView
  vh.textView.setText(...);

  // Hide the (potentially recycled) expand button until ellipsizing checked
  vh.expandBtn.setVisibility(GONE);

  Layout layout = vh.textView.getLayout();
  if (layout != null) {
    // The TextView has already been laid out
    // We can check whether it's ellipsized immediately
    if (layout.getEllipsisCount(layout.getLineCount()-1) > 0) {
      // Text is ellipsized in re-used view, show 'Expand' button
      vh.expandBtn.setVisibility(VISIBLE);
    }
  } else {
    // The TextView hasn't been laid out, so we need to set an observer
    // The observer fires once layout's done, when we can check the ellipsizing
    ViewTreeObserver vto = vh.textView.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
        Layout layout = vh.textView.getLayout();
        if (layout.getEllipsisCount(layout.getLineCount()-1) > 0) {
          // Text is ellipsized in newly created view, show 'Expand' button
          vh.expandBtn.setVisibility(VISIBLE);
        }

        // Remove the now unnecessary observer
        // It wouldn't fire again for reused views anyways
        ViewTreeObserver obs = vh.textView.getViewTreeObserver();
        obs.removeGlobalOnLayoutListener(this);
      }
    });

  return convertView;
}

I hope I understand your question correctly--if you're looking to end a TextView that's too wide for a screen with ellipses, you can add these attributes to your TextView:

android:ellipsize="end"
android:maxLines="1"
android:scrollHorizontally="true"

If, however, you want to determine whether a TextView is ended with an ellipsis or is displayed fully, I'm not so sure that's possible--it doesn't look like it is. Still, you might want to try the getEllipsize() method of TextView. I'm not sure whether that returns the point at where the TextView is ellipsized by Android, or where you have set the TextView to be ellipsized.

You can either set your text to marque. add this in your code might help.....

    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:scrollHorizontally="true"
    android:freezesText="true"
    android:marqueeRepeatLimit="marquee_forever"

You can also put a horizontal scrollview for you code....

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/list_item_description"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="1"
                android:ellipsize="end"
                android:gravity="center_vertical"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceSmall" />
        </LinearLayout>
    </HorizontalScrollView>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!