Sunday, 2 September 2018

android view width is not bigger when a11y is changed to big font

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)

enter image description here

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?



from android view width is not bigger when a11y is changed to big font

No comments:

Post a Comment