How to fix the Snackbar height and position?

thiagolr picture thiagolr · Dec 23, 2016 · Viewed 14.1k times · Source

On Android Support Library 24.1.1, the Snackbar was working fine:

before

Then starting on Android Support Library 24.2.0 onwards, the Snackbar started to behave like this:

after

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?

Answer

maxarmour picture maxarmour · May 2, 2017

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.