diff --git a/lib/java/com/google/android/material/snackbar/BaseTransientBottomBar.java b/lib/java/com/google/android/material/snackbar/BaseTransientBottomBar.java index e64fbe8ce8a..b791e76cd03 100644 --- a/lib/java/com/google/android/material/snackbar/BaseTransientBottomBar.java +++ b/lib/java/com/google/android/material/snackbar/BaseTransientBottomBar.java @@ -712,7 +712,7 @@ final void showView() { setUpBehavior((CoordinatorLayout.LayoutParams) lp); } - targetParent.addView(this.view); + this.view.addToTargetParent(targetParent); recalculateAndUpdateMargins(); // Set view to INVISIBLE so it doesn't flash on the screen before the inset adjustment is @@ -1111,6 +1111,7 @@ public boolean onTouch(View v, MotionEvent event) { private PorterDuff.Mode backgroundTintMode; @Nullable private Rect originalMargins; + private boolean addingToTargetParent; protected SnackbarBaseLayout(@NonNull Context context) { this(context, null); @@ -1233,7 +1234,10 @@ protected void onDetachedFromWindow() { @Override public void setLayoutParams(ViewGroup.LayoutParams params) { super.setLayoutParams(params); - if (params instanceof MarginLayoutParams) { + if (!addingToTargetParent && params instanceof MarginLayoutParams) { + // Do not update the original margins when the layout is being added to its target parent, + // since the margins are just copied from the existing layout params, which can already be + // updated with extra margins. updateOriginalMargins((MarginLayoutParams) params); if (baseTransientBottomBar != null) { baseTransientBottomBar.updateMargins(); @@ -1266,6 +1270,12 @@ int getMaxInlineActionWidth() { return maxInlineActionWidth; } + void addToTargetParent(ViewGroup targetParent) { + addingToTargetParent = true; + targetParent.addView(this); + addingToTargetParent = false; + } + private void setBaseTransientBottomBar(BaseTransientBottomBar baseTransientBottomBar) { this.baseTransientBottomBar = baseTransientBottomBar; }