Skip to content

Commit f86cedd

Browse files
pekingmehunterstich
authored andcommitted
[Button] Defer icon changes during width animation in MaterialButton.
This change introduces pending states for icon and icon size. When a width animation is active, calls to setIcon and setIconSize are deferred and applied only after the animation completes, preventing layout conflicts. PiperOrigin-RevId: 840343132
1 parent 0b669da commit f86cedd

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

lib/java/com/google/android/material/button/MaterialButton.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,19 @@ private SpringForce createSpringForce() {
344344
R.style.Motion_Material3_Spring_Standard_Fast_Spatial);
345345
}
346346

347+
private boolean maybeRunAfterWidthAnimation(Runnable action) {
348+
if (widthIncreaseSpringAnimation != null && widthIncreaseSpringAnimation.isRunning()) {
349+
post(
350+
() -> {
351+
action.run();
352+
recoverOriginalLayoutParams();
353+
requestLayout();
354+
});
355+
return true;
356+
}
357+
return false;
358+
}
359+
347360
@NonNull
348361
@SuppressLint("KotlinPropertyAccess")
349362
String getA11yClassName() {
@@ -864,6 +877,9 @@ public void setIconSize(@Px int iconSize) {
864877
}
865878

866879
if (this.iconSize != iconSize) {
880+
if (maybeRunAfterWidthAnimation(() -> setIconSize(iconSize))) {
881+
return;
882+
}
867883
originalWidth = UNSET;
868884
this.iconSize = iconSize;
869885
updateIcon(/* needsIconReset= */ true);
@@ -893,6 +909,9 @@ public int getIconSize() {
893909
*/
894910
public void setIcon(@Nullable Drawable icon) {
895911
if (this.icon != icon) {
912+
if (maybeRunAfterWidthAnimation(() -> setIcon(icon))) {
913+
return;
914+
}
896915
originalWidth = UNSET;
897916
this.icon = icon;
898917
updateIcon(/* needsIconReset= */ true);

0 commit comments

Comments
 (0)