From b0f37007aa3cd00cc655439ff84484354c478a53 Mon Sep 17 00:00:00 2001 From: conradchen Date: Tue, 8 Feb 2022 17:42:52 +0000 Subject: [PATCH] [Button] Fix icon position not being updated when size is not changed 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 https://github.com/material-components/material-components-android/issues/2553 PiperOrigin-RevId: 427215840 --- .../com/google/android/material/button/MaterialButton.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/java/com/google/android/material/button/MaterialButton.java b/lib/java/com/google/android/material/button/MaterialButton.java index 5fbf16c0165..1741c8dc745 100644 --- a/lib/java/com/google/android/material/button/MaterialButton.java +++ b/lib/java/com/google/android/material/button/MaterialButton.java @@ -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