On Android Support Library 24.1.1, the Snackbar was working fine:
Then starting on Android Support Library 24.2.0 onwards, the Snackbar started to behave like this:
On the library revision history, there is the following statement:
Behavior changes: Snackbar now draws behind the navigation bar if the status bar is translucent.
But the thing is that my app is full screen and it doesn't have the navigation bar or the status bar. How can I fix it?
I recently solved this by subtracting the navigation bar height from the bottom margin of the Snackbar view.
First we need the navigation bar height. I found code for that in the answer marked as correct here: How to REALLY get the navigation bar height in Android
Next, use the following code to adjust the Snackbar bottom margin:
Note that I used the LayoutParams of a CoordinatorLayout. You should replace CoordinatorLayout with whichever parent layout type you have passed in to your
Snackbar.make()
function (in my case,R.id.fullscreen_content
is a CoordinatorLayout). The nice thing about using CoordinatorLayout is that it allows Snackbars to be dismissed by swiping as a standard behavior.Another way would be
This will disable extra bottom padding in immersive mode.
If all you care about is the height, not the position, then prepare to have your mind blown! :D
Set your snackbar content to:
Look this answer https://stackoverflow.com/a/42180120/2550743 and just replace
to
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 theSnackbarLayout
(the container layout of theSnackbar
) so newer versions where theSnackbar
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 theSnackbar
look normal):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: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.
Use this in your snackbar,