-1

In my project i added the navhost fragment and BottomNavigationView to the my activity_main and everything seems fine when navigation but when i click the toolbar options i navigated to the another page but i still see the bottom navigation view, how can i not see the bottom navigation view? I added the navigation xml to navigate all the pages and created the bottom navigation menu on my resource file. The only problem is when i navigate to another page inside the bottom navigation pages i still see the bottom navigation view.

<androidx.appcompat.widget.Toolbar
    android:id="@+id/app_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">


    <TextView
        android:id="@+id/app_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:contentDescription="@string/text_some"
        android:fontFamily="@font/lobster_two"
        android:text="@string/text_some"
        android:textColor="@color/colorSecondary"
        android:textSize="24sp" />
</androidx.appcompat.widget.Toolbar>


<fragment
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/app_toolbar"
    app:layout_constraintVertical_bias="0.0"
    app:navGraph="@navigation/bottom_navigation_graph" />

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/bottom_navigation_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="0dp"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        android:showAsAction="always|withText"
        app:itemIconSize="22dp"
        app:itemIconTint="@drawable/bottom_nav_color"
        app:itemTextColor="@drawable/bottom_nav_color"
        app:labelVisibilityMode="labeled"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/circle_view_image"
        android:layout_width="66dp"
        android:layout_height="66dp"
        android:layout_marginBottom="25dp"
        android:elevation="99dp"
        android:src="@drawable/image"
        app:layout_constraintBottom_toBottomOf="@+id/bottom_nav"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

2 Answers2

0

when i click the toolbar options i navigated to the another page

I am not sure what this means, but I assume that this toolbar icon click starts a global action that navigates to another Fragment, where you want to hide the Bottom Navigation.

In order hide the Bottom Navigation in certain fragments, do this in your MainActivity:

  navController.addOnDestinationChangedListener { _, nd: NavDestination, _->
    if (nd.id == R.id.fragmentWithNoBottomNav){
        bottom_nav.visibility = View.GONE
    } else {
        bottom_nav.visibility = View.VISIBLE
    }
Gil Becker
  • 283
  • 1
  • 9
0

I have been struggling with this issue for sometime, And I ended up with this approach: In your MainActivity

companion object {
    private val BOTTOM_NAV_VISIBLE_FRAGMENTS =
        listOf(
            R.id.Fragment1,
            R.id.Fragment2,
            R.id.Fragment3,
            R.id.Fragment4,
        )
    private const val EXIT_DURATION = 250L
    private const val ENTER_DURATION = 250L
}

define a getter function for BottomNavigationView visibility

    private val isBottomNavigationViewVisible: Boolean
    get() = bottom_nav.visibility == View.VISIBLE

Then in your onCreate function:

navController.addOnDestinationChangedListener { _, destination, b ->
        invalidateOptionsMenu()
        when (destination.id) {
            in BOTTOM_NAV_VISIBLE_FRAGMENTS -> {
                if (!isBottomNavigationViewVisible) showBottomNavigation()
            }
         
            else -> {
                if (isBottomNavigationViewVisible) hideBottomNavigation()
            }
        }
    }

consider you switch from fragment1 to fragment2, Both have bottom_nav.visibility sets to true, those if statements prevent animations being run if previous fragment and current fragment both have the same visibility for bottom_nav ( both View.Gone or View.Visible )

Then you can define two functions: showBottomNavigationView and hideBottomNavigationView so you are able to set animations and ... if you like

For example:

    private fun hideBottomNavigation() {
    with(bottom_nav) {
        if (visibility == View.VISIBLE && alpha == 1f) {
            animate()
                .alpha(0f)
                .withEndAction { visibility = View.GONE }
                .duration = EXIT_DURATION
        }
    }
}

private fun showBottomNavigation() {
    with(bottom_nav) {
        visibility = View.VISIBLE
        animate()
            .alpha(1f)
            .duration = ENTER_DURATION
    }
}

With this approach, If you want to show bottomNavigationView in another fragment, You just need to add that fragment ID to the BOTTOM_NAV_VISIBLE_FRAGMENTS list, if you want to change hide/show animations you simply add that to the show/hideBottomNavigationView functions.

Hope that helps :)

ArMot
  • 148
  • 7