Skip to content Skip to sidebar Skip to footer

Hiding Bottom Navigation View On Android

I'm developing an Android app that follows the single activity pattern. In one of my fragments I have a RecyclerView and in the case where the user scrolls down I want to hide my B

Solution 1:

Preview output

Create kotlin class NavigationViewBehavior.kt

import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.ViewCompat
import androidx.core.view.ViewCompat.NestedScrollType
import com.google.android.material.bottomnavigation.BottomNavigationView

classNavigationViewBehavior : CoordinatorLayout.Behavior<BottomNavigationView>() {
    privatevar height = 0overridefunonLayoutChild(parent: CoordinatorLayout, child: BottomNavigationView, layoutDirection: Int): Boolean {
        height = child.height
        returnsuper.onLayoutChild(parent, child, layoutDirection)
    }

    overridefunonStartNestedScroll(coordinatorLayout: CoordinatorLayout,
                                     child: BottomNavigationView, directTargetChild: View, target: View,
                                     axes: Int, type: Int): Boolean {
        return axes == ViewCompat.SCROLL_AXIS_VERTICAL
    }

    overridefunonNestedScroll(coordinatorLayout: CoordinatorLayout, child: BottomNavigationView,
                                target: View, dxConsumed: Int, dyConsumed: Int,
                                dxUnconsumed: Int, dyUnconsumed: Int,
                                @NestedScrollType type: Int) {
        if (dyConsumed > 0) {
            slideDown(child)
        } elseif (dyConsumed < 0) {
            slideUp(child)
        }
    }

    privatefunslideUp(child: BottomNavigationView) {
        child.clearAnimation()
        child.animate().translationY(0f).duration = 200
    }

    privatefunslideDown(child: BottomNavigationView) {
        child.clearAnimation()
        child.animate().translationY(height.toFloat()).duration = 200
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/business_container"android:layout_width="match_parent"android:layout_height="match_parent"android:hardwareAccelerated="true"><androidx.coordinatorlayout.widget.CoordinatorLayoutandroid:id="@+id/coordinator_business_layout"android:layout_width="0dp"android:layout_height="0dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"><FrameLayoutandroid:id="@+id/frame_main"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginBottom="?actionBarSize"android:orientation="vertical"><fragmentandroid:id="@+id/nav_host_fragment"android:name="androidx.navigation.fragment.NavHostFragment"android:layout_width="match_parent"android:layout_height="match_parent"app:defaultNavHost="true"app:navGraph="@navigation/mobile_navigation"tools:ignore="FragmentTagUsage" /></FrameLayout><com.google.android.material.bottomnavigation.BottomNavigationViewandroid:id="@+id/nav_view"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:layout_gravity="bottom"android:background="@color/colorWhite"app:itemBackground="@drawable/border_bottom_ver"app:itemIconTint="@drawable/tab_color"app:itemTextColor="@drawable/tab_color"app:menu="@menu/bottom_nav_menu" /></androidx.coordinatorlayout.widget.CoordinatorLayout></androidx.constraintlayout.widget.ConstraintLayout>

call in MainActivity.kt

val navController = Navigation.findNavController(this,
                    R.id.nav_host_fragment)
            NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration!!)
            NavigationUI.setupWithNavController(navView!!, navController)
            val layoutParams = navView!!.getLayoutParams() as CoordinatorLayout.LayoutParams
            layoutParams.behavior = NavigationViewBehavior()
            val params = frameLayout.layoutParams as ViewGroup.MarginLayoutParams

            //hide bottom navigation no need while sun activities are being used
            navController.addOnDestinationChangedListener { controller: NavController?,
                                                            destination: NavDestination, arguments: Bundle? ->
                if (destination.id == R.id.navigation_fragments ) {
                    params.setMargins(0, 0, 0, 0)
                    frameLayout.layoutParams = params
                    navView!!.setVisibility(View.GONE)
                } else {
                    params.setMargins(0, 0, 0, 160)
                    frameLayout.layoutParams = params
                    navView!!.setVisibility(View.VISIBLE)
                }
            }

home_fragment.xml

<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/rvServices"android:layout_width="match_parent"android:layout_height="match_parent"android:scrollbars="vertical"android:visibility="visible" /><TextViewandroid:id="@+id/no_values_assigned_message"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center_horizontal"android:text="@string/no_values_assigned"android:textSize="@dimen/fontSizeReg"android:textStyle="bold"android:visibility="gone" /></androidx.constraintlayout.widget.ConstraintLayout>

this is the setting my RecyclerView

privatefunRecyclerSetting(view: View) {
        no_value = view.findViewById(R.id.no_values_assigned_message)
        rvItem = view.findViewById(R.id.rvServices)
        rvItem!!.setHasFixedSize(true)

        //Very IMP This Lineval manager = GridLayoutManager(activity, 2,
                GridLayoutManager.VERTICAL,
                false)
        data
        rvItem!!.setLayoutManager(manager)
    }

Visible

Hidden also specific fragment works

while you scroll recycler view auto-hide and also you can hide bottom navigation from specific Fragment code included

Post a Comment for "Hiding Bottom Navigation View On Android"