Search code examples
androidbottomnavigationviewandroid-snackbar

Change position of Snackbar while it is shown based on BottomNavigationView's visibility


I have a BottomNavigationView within a CoordinatorLayout in my activity. I have a Snackbar that is displayed above the BottomNav so all good there. Based on some conditions the BottomNav visibility is modified. However the snackbar does not resize to the bottom of the screen. It stays on the top of the now gone BottomNav

I tried observing on the visibility of the bottom bar and drawing a new one when the visibility changes. However the dismiss occurs after I draw the new snackBar, which dismisses it.

        bottomNavigationView.viewTreeObserver.addOnGlobalLayoutListener {
            val newVis = bottomNavigationView.visibility
            if (bottomNavigationView.tag as Int != newVis) {
                bottomNavigationView.tag = bottomNavigationView.visibility
                if (p2NotificationSnackBar != null && p2NotificationSnackBar!!.isShown) {
                    Logger.d(TAG, "BottomNavigationBar visibility changed")

                    val snackBarTextView = p2NotificationSnackBar!!.view.findViewById(android.support.design.R.id.snackbar_text) as TextView
                    val snackBarText = snackBarTextView.text.toString()
                    p2NotificationSnackBar!!.dismiss()
                    p2NotificationCount = currentMessageCount
                    displayP2Notification(message = snackBarText, redrawSnackBar = true)
                }
            }
        }

Ideally I'd like the snackbar to move to the bottom without needing redraw. Thanks


Solution

  • The only API that worked was the setY API that belongs to the View class. Get the view of the snackbar and call the setY value. It instantly moves to the new Y value

    snackbar.view.setY = 0F