Search code examples
androidtextviewfont-sizeandroid-fontsandroid-chips

Manually calculating available width is incorrect when display and font sizes are large


I have a ViewGroup with a Chip as a child with no horizontal siblings.

I try to calculate the available width before choosing which text to set inside the Chip.

final View parent = (View) getParent();
final int parentPaddingLeft = parent.getPaddingLeft();
final int parentPaddingRight = parent.getPaddingRight();
final int paddingLeft = getPaddingLeft();
final int paddingRight = getPaddingRight();
final int leftMargin = layoutParams.leftMargin;
final int rightMargin = layoutParams.rightMargin;
final int textViewSafetyPadding = 20;
final int currentTextMaxWidth =
    parentWidth
        - (textViewSafetyPadding
            + parentPaddingLeft
            + parentPaddingRight
            + paddingLeft
            + paddingRight
            + leftMargin
            + rightMargin);

It all worked well, but when I change the OS display size and font size to the largest - I see the chosen text doesn't fit inside the Chip.

Even though the paint measurement and chip actual width show it should fit.

Chip's width onDraw == 550pxl chosen text requires 442pxl

and

paint.measureText(chipText) <= textMaxWidth

Any idea of abnormal extra padding when font size and display size are bigger than default?


Solution

  • Chip is a compoundButton, I should have subtracted Chip#getCompoundPaddingRight/Left instead of getPaddingRight/Left.