I'm trying to create a single activity Android application. I have MainActivity (only activity) with BottomNavigationView, three top level fragments and some child fragments. My requirement is whenever the screen is showing top level fragments, bottom navigation should be visible such that switching is possible. But when I'm viewing any of the child fragments, bottom navigation should be hidden. Is there any out-of-box way using the Navigation component or need to change the visibility manually ?
On Android 4.1 and higher, you can set your application's content to appear behind the navigation bar, so that the content doesn't resize as the navigation bar hides and shows. To do this, use SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION .
Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”. Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.
Update (Navigation component 1.0)
As of Navigation component 1.0.0-alpha08, method addOnNavigatedListener(controller: NavController, destination: NavDestination)
was changed to addOnDestinationChangedListener(controller: NavController, destination: NavDestination, arguments: Bundle)
. Its behavior was also slightly changed (it is also called if the destinations arguments change).
Old Answer
You can use NavController.OnNavigatedListener to achieve this behavior (set it in Activity onCreate):
findNavController(R.id.container).addOnNavigatedListener { _, destination -> when (destination.id) { R.id.dashboardFragment -> showBottomNavigation() else -> hideBottomNavigation() } } private fun hideBottomNavigation() { // bottom_navigation is BottomNavigationView with(bottom_navigation) { if (visibility == View.VISIBLE && alpha == 1f) { animate() .alpha(0f) .withEndAction { visibility = View.GONE } .duration = EXIT_DURATION } } } private fun showBottomNavigation() { // bottom_navigation is BottomNavigationView with(bottom_navigation) { visibility = View.VISIBLE animate() .alpha(1f) .duration = ENTER_DURATION } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With