Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Snackbar behind navigation bar

In landscape mode, I am using

    window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

to show and hide navigation bar.

When I create Snackbar while navigation is showing

Snackbar bar  = Snackbar.make(snackbarContainer, "exiting", Snackbar.LENGTH_INDEFINITE);
    bar.setAction("Testing", new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //no-op
        }
    });
    bar.show();

The Snackbar's action text is being covered by the navigation bar. Is there a way to adjust snackbar's width automatically based on whether the navigation bar is showing or not?

like image 825
chocobo Avatar asked Apr 13 '16 04:04

chocobo


2 Answers

Possibly you are searching for this:

private void displaySnackBarWithBottomMargin(Snackbar snackbar, int sideMargin, int marginBottom) {
    final View snackBarView = snackbar.getView();
    final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) snackBarView.getLayoutParams();

    params.setMargins(params.leftMargin + sideMargin, params.topMargin, params.rightMargin + sideMargin, params.bottomMargin + marginBottom);

    snackBarView.setLayoutParams(params);
    snackbar.show();
}

Actually found it at: https://stackoverflow.com/a/34844669/371749

If you add the margin bottom from the navigation bar height (48dp) it perfectly appears above the navigation bar.

like image 59
cV2 Avatar answered Oct 20 '22 16:10

cV2


You could add a CoordinatorLayout (for instance when using a ConstraintLayout, tie it to the bottom of the parent).

Next, give the id of the CoordinatorLayout to the Snackbar.make(...) function (or any child in the CoordinatorLayout).

A snackbar will iterate the views from the current one up until it finds a CoordinatorLayout and attach itself to it.

like image 2
Boy Avatar answered Oct 20 '22 14:10

Boy