Doilio Matsinhe
Doilio Matsinhe

Reputation: 2280

How to hide the bottom navigation bar in certain fragments?

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
        }

Upvotes: 15

Views: 10432

Answers (2)

SKYRELA
SKYRELA

Reputation: 31

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
}

Upvotes: 1

Artem Botnev
Artem Botnev

Reputation: 2427

For your fragment where it should be visible

navView.visibility = View.VISIBLE

Where it shouldn't be visible

navView.visibility = View.GONE

Upvotes: 7

Related Questions