I have some code that measure the width of an android textView and the text to display in it.
final ViewTreeObserver[] viewTreeObserver = {myAccountView.getViewTreeObserver()};
    viewTreeObserver[0].addOnPreDrawListener(
        new OnPreDrawListener() {
          @Override
          public boolean onPreDraw() {
            myAccountView.setText(R.string.og_my_account_desc_long_length);
            int chipWidth = myAccountView.getMeasuredWidth();
            if (chipWidth > 0) {
              setChipTextWithCorrectLength(chipWidth);
              viewTreeObserver[0] = myAccountView.getViewTreeObserver();
              if (viewTreeObserver[0].isAlive()) {
                viewTreeObserver[0].removeOnPreDrawListener(this);
              }
            }
            return true;
          }
        });
  }
  private void setChipTextWithCorrectLength(int chipWidth) {
    String desc =
        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);
    myAccountView.setText(desc);
  }
  @VisibleForTesting
  String setChipTextWithCorrectLength(
      String longDesc, String mediumDesc, String shortDesc, int clipWidth) {
    if (textWidthPixels(longDesc) > clipWidth) {
      if (textWidthPixels(mediumDesc) > clipWidth) {
        return shortDesc;
      } else {
        return mediumDesc;
      }
    }
    return longDesc;
  }
  public float textWidthPixels(String text) {
    TextPaint textPaint = myAccountView.getPaint();
    return textPaint.measureText(text);
  }
I changed my device a11y to the biggest font and biggest display.
I see in the UI the text is truncated (ellipsized)
but my measurements show the text width (503 pixels) is smaller than the text-view width (587 px).
how can it be?
how should i measure them differently so my code will also indicate the text width is bigger than the text-view width?
Edit:
i have tried to add padding, but it didn't change. changing to a11y truncated the long text instead of choosing shorter text.
  public float textWidthPixels(String text) {
    TextPaint textPaint = myAccountView.getPaint();
    View parent = (View) myAccountView.getParent();
    float width = textPaint.measureText(text);
    int paddingLeft = parent.getPaddingLeft();
    int paddingRight = parent.getPaddingRight();
    return width - (paddingLeft + paddingRight);
  }
Edit:
i have tried to calculate chipWidth considering its paddings, but still textSize make the text truncate while it's drawn size in code comes shorter than the view size
int chipWidth = myAccountView.getMeasuredWidth() - myAccountView.getPaddingLeft() - myAccountView.getPaddingRight();

 
     
    