Remeasure snackbar inner layout if it's MATCH_PARENT in width
Test: all the tests pass, also visual inspection on tablets
Bug: 34186126
Change-Id: I135136563647261cd842e1aec2ae9b78e1ca6901
diff --git a/design/src/android/support/design/widget/Snackbar.java b/design/src/android/support/design/widget/Snackbar.java
index a096a3d..82add6f 100644
--- a/design/src/android/support/design/widget/Snackbar.java
+++ b/design/src/android/support/design/widget/Snackbar.java
@@ -324,6 +324,25 @@
public SnackbarLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
+
+ @Override
+ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+ super.onMeasure(widthMeasureSpec, heightMeasureSpec);
+ // Work around our backwards-compatible refactoring of Snackbar and inner content
+ // being inflated against snackbar's parent (instead of against the snackbar itself).
+ // Every child that is width=MATCH_PARENT is remeasured again and given the full width
+ // minus the paddings.
+ int childCount = getChildCount();
+ int availableWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
+ for (int i = 0; i < childCount; i++) {
+ View child = getChildAt(i);
+ if (child.getLayoutParams().width == ViewGroup.LayoutParams.MATCH_PARENT) {
+ child.measure(MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.EXACTLY),
+ MeasureSpec.makeMeasureSpec(child.getMeasuredHeight(),
+ MeasureSpec.EXACTLY));
+ }
+ }
+ }
}
}