To my surprise, Snackbar natively appears to set extra padding on the TextView if it spans multiple lines. It effectively doubles the height of the Snackbar, and for some reason only affects vertical padding. I found this in the SnackbarContentLayout.onMeasure
:
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mMaxWidth > 0 && getMeasuredWidth() > mMaxWidth) {
widthMeasureSpec = MeasureSpec.makeMeasureSpec(mMaxWidth, MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
final int multiLineVPadding = getResources().getDimensionPixelSize(
R.dimen.design_snackbar_padding_vertical_2lines);
final int singleLineVPadding = getResources().getDimensionPixelSize(
R.dimen.design_snackbar_padding_vertical);
final boolean isMultiLine = mMessageView.getLayout().getLineCount() > 1;
boolean remeasure = false;
if (isMultiLine && mMaxInlineActionWidth > 0
&& mActionView.getMeasuredWidth() > mMaxInlineActionWidth) {
if (updateViewsWithinLayout(VERTICAL, multiLineVPadding,
multiLineVPadding - singleLineVPadding)) {
remeasure = true;
}
} else {
final int messagePadding = isMultiLine ? multiLineVPadding : singleLineVPadding;
if (updateViewsWithinLayout(HORIZONTAL, messagePadding, messagePadding)) {
remeasure = true;
}
}
if (remeasure) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
Why does Snackbar do this? Why isn't it mentioned at all in the documentation? What's more is that it seems like this can't be overridden even after invalidating the views and requesting layout. Is there a way to work around this without creating a custom view?
EDIT: Looking back at the material design docs for the Snackbar, there are examples of the multi-lined snackerbars without the additional vertical padding, which is what I'm aiming for. The code above seems to undermine the intended material design for multi-lined messages. For reference: https://material.io/components/snackbars/#anatomy
Currently (1.1.0-beta01 and 1.2.0-alpha01) there isn't an official way to customize the padding with 2 lines.
The only workaround (but it can stop to work in the next releases) is to define in the dimens.xml
<dimen name="design_snackbar_padding_vertical_2lines">14dp</dimen>
or
<dimen name="design_snackbar_padding_vertical_2lines">@dimen/design_snackbar_padding_vertical</dimen>
Before and after: