15

I have An activity with a navGraph and a bottom Navigation bar with 2 menu items. My problem is that My Bottom Navigation Bar appears everywhere, detailFragment, aboutFragment, signInFragment and so on.


        val navController = this.findNavController(R.id.myNavHostFragment)

        val appBarConfiguration = AppBarConfiguration.Builder(
            R.id.contactsFragment,
            R.id.profileFragment
        ).build()

        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration)

        val navView: BottomNavigationView = findViewById(R.id.nav_view)
        NavigationUI.setupWithNavController(navView, navController)


How do i Limit it to just show on the 2 fragments on my menu Item?

This is how I solved It

    navController.addOnDestinationChangedListener{ _, nd: NavDestination, _->
        if(nd.id == R.id.contactsFragment || nd.id == R.id.profileFragment){
            navView.visibility = View.VISIBLE
        }else{
            navView.visibility = View.GONE
        }
Doilio Matsinhe
  • 2,131
  • 16
  • 29

2 Answers2

7

For your fragment where it should be visible

navView.visibility = View.VISIBLE

Where it shouldn't be visible

navView.visibility = View.GONE
Artem Botnev
  • 2,267
  • 1
  • 14
  • 19
1

Are you trying to hide bottom navigation view from your current fragment, that is in main activity or parent activity. Well it's that easy. Just go to the fragment where you don't want bottom navigation view and paste the following code bellow onCreateView.

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    val view = requireActivity().findViewById<BottomNavigationView>(R.id.nav_view)

    val fab = requireActivity().findViewById<FloatingActionButton>(R.id.fab)
    view.visibility = View.GONE
    fab.visibility = View.GONE
}
SKYRELA
  • 21
  • 3