Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide android bottom navigation view for child screens/ fragments

Tags:

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 ?

like image 345
Subhojit Shaw Avatar asked Aug 21 '18 19:08

Subhojit Shaw


People also ask

How do I hide the bottom navigation bar in fragments?

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 .

How do I hide the bottom navigation bar on Android?

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.


1 Answers

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     } } 
like image 111
artnest Avatar answered Oct 08 '22 20:10

artnest