'How do I correctly anchor Snackbar in android, so it does not cover my bottom navigation view?

I am trying to avoid the snackbar covering the bottom navigation view. Instead, it should only appear inside the nav_host_fragment, but for some reason, it does not anchor onto the fragment (R.id.nav_host_fragment). Is there a way to solve this without changing my layout.xml too much?

Activity onCreate

Snackbar.make(findViewById(R.id.nav_host_fragment)!!, "No data available", Snackbar.LENGTH_SHORT).apply {
            anchorView = findViewById(R.id.nav_host_fragment)!!
        }.show()

Layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/after_login_activity"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".view.AfterLoginActivity"
    >

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/bottomNavigationView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        app:itemIconSize="28dp"
        app:labelVisibilityMode="unlabeled"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>


Solution 1:[1]

You could possibly add a CoordinatorLayout between the fragment and bottomNav, then attach the Snackbar to the CoordinatorLayout instead of the nav_host_fragment.

Another thing you could try is create LayoutParams and apply them to the snackbar's view before showing it. Something like the following:

    val snackbar: Snackbar = Snackbar.make(navHostFragment, "No data available", Snackbar.LENGTH_SHORT)
    val snackbarView: View = snackbar.view
    val lp = LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT,
        LinearLayout.LayoutParams.WRAP_CONTENT
    )
    lp.setMargins(left, top, right, bottom) // add your desired values
    snackbarView.setLayoutParams(lp)
    snackbar.show()

This might reposition the snackbar.

Solution 2:[2]

Utiliza .setAnchorView

            Snackbar.make(mBinding.root, message, duration)
                .setAnchorView(mBinding.bottomNav)
                .setAnimationMode(ANIMATION_MODE_SLIDE)
                .setBackgroundTint(Color.parseColor(SUCCESS_COLOR))
                .show()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Filip Petrovski
Solution 2 LeoCapitan