Skip to content

Commit

Permalink
[Button] Fix icon position not being updated when size is not changed
Browse files Browse the repository at this point in the history
If a button has a WRAP_CONTENT width and after its text is updated but its size does not change, the icon position won't be updated because at the moment when onTextChanged() is called, getLayout() will return null and a new layout to display text will only be recreated in the next onMeasure() call.

To fix this issue, the change moves updateIconPosition() from onSizeChanged() to onLayout(), which will always be called after onMeasure() or onSizeChanged() is called, and thus covers both of the cases after the text is updated (the button changes its size or not.)

Note: we still need to keep the updateIconPosition() call in onTextChanged(), to cover the case that the button has a fixed width, in which updating text won't trigger remeasuring of the button.

Resolves #2553

PiperOrigin-RevId: 427215840
  • Loading branch information
drchen authored and raajkumars committed Feb 9, 2022
1 parent f3c6430 commit b0f3700
Showing 1 changed file with 1 addition and 6 deletions.
Expand Up @@ -448,12 +448,7 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto
if (VERSION.SDK_INT == VERSION_CODES.LOLLIPOP && materialButtonHelper != null) {
materialButtonHelper.updateMaskBounds(bottom - top, right - left);
}
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
updateIconPosition(w, h);
updateIconPosition(getMeasuredWidth(), getMeasuredHeight());
}

@Override
Expand Down

0 comments on commit b0f3700

Please sign in to comment.