Search code examples
androidandroid-snackbarsnackbarandroid-navigation-bar

Can I anchor a snackbar to Android native bottom navigation bar?


I want to anchor a snackbar to the Android native bottom navigation bar.

As I saw the documentation:

Anchoring a Snackbar By default, Snackbars will be anchored to the bottom edge of their parent view. However, you can use the Snackbar#setAnchorView method to make a Snackbar appear above a specific view within your layout, e.g., a FloatingActionButton. This is especially helpful if you would like to place a Snackbar above navigational elements at the bottom of the screen, such as a BottomAppBar or BottomNavigationView.

I saw this post on how to get the Android native bottom navigation bar.

int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
        return id > 0 && resources.getBoolean(id);

But then, is it just a boolean I can get and no the id of the view itself?


Solution

  • From what I understand you need to place your snackbar above your bottom navigation, all you need to do is :

    val bottomNavigation: BottomNavigationView = findViewById(R.id.bottom_navigation)
    Snackbar.make(it, "Hello World", Snackbar.LENGTH_LONG).setAnchorView(bottomNavigation).show()
    

    Note: A nice documentation about Snackbars and Android design in general.