The accepted answer worked with older versions of the support libraries where the Snackbar
was just a rectangular view. What is actually happening by changing the margin to a negative value is just cutting off the bottom of the SnackbarLayout
(the container layout of the Snackbar
) so newer versions where the Snackbar
has rounded corners look bad with this solution.
The clue is in the code here: https://github.com/material-components/material-components-android/blob/cd59e98f7e2185ddb075ff0fc91f29765d562968/lib/java/com/google/android/material/snackbar/BaseTransientBottomBar.java#L272
What is actually happening is that padding is being added to the container, so the way to correctly fix the height is to reset the padding to the correct amount. You can do this by adding an additional OnApplyWindowInsetsListener
such as the following (setting the bottom padding to the same as the top makes the Snackbar
look normal):
ViewCompat.setOnApplyWindowInsetsListener(snackbar.view) { v, insets ->
v.setPadding(v.paddingLeft, v.paddingTop, v.paddingRight, v.paddingTop)
insets
}
Then, as the Snackbar
will now be the correct height but appear behind a translucent nav bar, you can increase the bottom margin by the value of the bottom inset:
ViewCompat.setOnApplyWindowInsetsListener(view) { v, insets ->
v.setPadding(v.paddingLeft, v.paddingTop, v.paddingRight, v.paddingTop)
val params = v.layoutParams as ViewGroup.MarginLayoutParams
params.updateMargins(
params.leftMargin,
params.topMargin,
params.rightMargin,
params.bottomMargin + insets.systemWindowInsetBottom
)
v.layoutParams = params
insets
}
This has actually been fixed in the Material 1.1.0 alpha libraries (the library now changes the margin rather than the padding), but they are probably not ready for production use yet.