How to fix the Snackbar height and position?
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:
final Snackbar snackbar = Snackbar.make(findViewById(R.id.fullscreen_content),
message, Snackbar.LENGTH_LONG);
View snackbarView = snackbar.getView();
// Adjust Snackbar height for fullscreen immersive mode
int navbarHeight = getNavigationBarSize(this).y;
CoordinatorLayout.LayoutParams parentParams = (CoordinatorLayout.LayoutParams) snackbarView.getLayoutParams();
parentParams.setMargins(0, 0, 0, 0 - navbarHeight);
snackbarView.setLayoutParams(parentParams);
snackbar.show();
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.
It's 2020 and also I don't know if it's related but I got my snackbar having some padding at the bottom when using android 10 gesture navigation. None of the above works for my case. I finally got it fixed with a super simple line:
val snackbar = Snackbar.make(view, message, duration)
snackbar.isGestureInsetBottomIgnored = true // here
snackbar.show()
Hope it helps.