androidandroid-snackbarandroid-navigation-bar

How would can I make the snackbar to be above the navigation bar? (y axis)


I have this layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/non_clickable_account_snackbar_constraint_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  <android.support.v7.widget.Toolbar
      android:id="@+id/my_snackbar_toolbar"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent" />
  <com.google.android.material.button.MaterialButton
      android:id="@+id/my_snackbar_button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:text="@string/show_account_snackbar"
      android:theme="@style/Theme.GoogleMaterial.DayNight.Bridge"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

I have a customized class:

public final class MySnackbar<AccountT>
    extends BaseTransientBottomBar<MySnackbar<myT>> {

with

super(findTopMostFrameLayout(parent), content, new MySnackbarAnimation(content));

and

when calling "show()", it shows an android snackbar with customized layout.

enter image description here

When I show a my_snackbar it's seen below the bottom navigation bar (z axis).

1) How can I make it on-top of the navigation bar (z-axis)? Or that's the common behavior?

2) How would you make it to be seen top to the bottom navigation bar? (y axis)


Solution

  • You likely are having your layout fit to system windows. Although I don't see it in your example, that is exactly what it does. Perhaps it can be set elsewhere as well.

    System windows are the parts of the screen where the system is drawing either non-interactive (in the case of the status bar) or interactive (in the case of the navigation bar) content.

    Most of the time, your app won’t need to draw under the status bar or the navigation bar, but if you do: you need to make sure interactive elements (like buttons) aren’t hidden underneath them. That’s what the default behavior of the android:fitsSystemWindows="true" attribute gives you: it sets the padding of the View to ensure the contents don’t overlay the system windows.

    That means it will also go under the navigation bar at the bottom and the system bar at the top.

    There is an easy fix to this. You can apply window insets.

    Chris Banes has some nice examples in that blog.

    snackbarCustomLayout.setOnApplyWindowInsetsListener { view, insets ->
        view.updatePadding(bottom = insets.systemWindowInsetBottom)
        insets
    }
    

    Bear in mind that you might also need to apply left and / or right insets to deal with landscape modes.