textView change to shorter text dynamically instead of ellipsize

421 Views Asked by At

I have this code:

  final ViewTreeObserver[] viewTreeObserver = {myAcco    
    viewTreeObserver[0].addOnPreDrawListener(
        new OnPreDrawListener() {
          @Override
          public boolean onPreDraw() {
            int chipWidth =
                myAccountView.getMeasuredWidth()
                    - myAccountView.getPaddingLeft()
                    - myAccountView.getPaddingRight();
            if (chipWidth > 0) {
              myAccountView.setText(
                  setChipTextWithCorrectLength(
                      getContext().getString(R.string.og_my_account_desc_long_length),
                      getContext().getString(R.string.og_my_account_desc_meduim_length),
                      getContext().getString(R.string.og_my_account_desc_short_length),
                      chipWidth));
              viewTreeObserver[0] = myAccountView.getViewTreeObserver();
              if (viewTreeObserver[0].isAlive()) {
                viewTreeObserver[0].removeOnPreDrawListener(this);
              }
            }
            return true;
          }
        });
  }

  @VisibleForTesting
  String setChipTextWithCorrectLength(
      String longDesc, String mediumDesc, String shortDesc, int clipWidth) {
    if (!isTextEllipsized(longDesc, clipWidth)) {
      return longDesc;
    }
    if (!isTextEllipsized(mediumDesc, clipWidth)) {
      return mediumDesc;
    }
    return shortDesc;
  }

  private boolean isTextEllipsized(String text, int clipWidth) {
    TextPaint textPaint = myAccountView.getPaint();

    Toast.makeText(this.getContext(), "textPaint.measureText(text) = "+textPaint.measureText(text)+" clipWidth ="+ clipWidth,
        Toast.LENGTH_LONG).show();


    return textPaint.measureText(text) > clipWidth;
  }

The code should change the text in a textView dynamically when ellipsize is needed.

However, when i run the application I see sometimes the view (clip) width == the long text width and there is still ellipsize in the UI.

I see that happens when the textView is not visible and toggeled to be visible.

Should I calculate the vlip (view) width differently?

Any special way to measure textView before becomes visible?

2

There are 2 best solutions below

4
On

You should check with

getEllipsisCount()

to see the source of the problem.

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

0
On

Yes you can use textView.getLayout().getEllipsisStart(0) ,make sure your textView is singleline , you can do this by adding android:maxLines="1" to your texView. It return the index from which the text gets truncated else returns 0.

Depending on your observation , it might be that the view is not recalculating after changing the visibility to visible again, try to use textView.setVisibility(View.GONE) instead of textView.setVisibility(View.INVISIBLE); .This might cause the preDraw() to be called again.

You might also consider using addOnGlobalLayoutListener() to keep the track of visibilty changes of your view, for reference.