0

I've a single Activity with a BottomNavigationView inside of it's layout:

<androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <androidx.fragment.app.FragmentContainerView
            android:id="@+id/nav_host"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />


        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
            app:menu="@menu/menu_home_bottom_navigation"
            />
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

My bottom_avigation changes nav_host FragmentContainerView with fragments. All of this fragments have NestedScrollView or RecyclerView and because of app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior", my bottom_navigation automatically hides/shows on scrollDown/scrollUp.

I saw this question: Hide/Show bottomNavigationView on Scroll . I'm currently using the answer given by Abhishek Singh but the problem is not this.

This is my problem: Imagine FragA and FragB both have RecyclerViews but FragA has less items causing that all items fit to the screen and not scrollable. Now when I switch from FragA to FragB and then scrollDown, bottom_navigation hides with animation and if I press back button I cannot see bottom_navigation anymore and because FragA is not scrollable I cannot make it visible by scrolling.

I've also tried bottom_navigation.visibility = View.Visible in FragA onResume event, but still does not work. I think that it somehow translates bottom_navigation to the bottom and because of that this code does not help.

So how can I fix this issue?

Saman Sattari
  • 3,322
  • 6
  • 30
  • 46
  • Does this answer your question? [Hide/Show bottomNavigationView on Scroll](https://stackoverflow.com/questions/44777869/hide-show-bottomnavigationview-on-scroll) – Laurentiu Daniel Sep 14 '20 at 06:13
  • @LaurentiuDaniel no, its just a solution for auto hide/show of bottomNavigation. I'm using it right now. – Saman Sattari Sep 14 '20 at 06:23

2 Answers2

0

Since there is no part from your code here my solution would be to listen on the back button:

maybe you can check this article it would be helpful

Android: onBackPressed() for Fragments

And there change the visibility for the BottomNavigationView.

0

I found the answer. instead of changing the visibility property of the bottom_navigation, I wrote two extension functions on BottomNavigationView for hiding/showing it:

private fun BottomNavigationView.showUp() {
    animate().setDuration(200L).translationY(0f).withStartAction { visibility = View.VISIBLE }.start()
}

private fun BottomNavigationView.hideDown() {
    animate().setDuration(200L).translationY(height.toFloat()).withEndAction { visibility = View.GONE }.start()
}

Now in onResume of FragA I have this:

override onResume() {
    super.onResume()
    bottom_navigation.showUp()
}
Saman Sattari
  • 3,322
  • 6
  • 30
  • 46