How to hide bottom nav bar in fragment

navController.addOnDestinationChangedListener { _, destination, _ ->
   if(destination.id == R.id.full_screen_destination) {
       
       bottomNavigationView.visibility = View.GONE
   } else {
       
       bottomNavigationView.visibility = View.VISIBLE
   }
}

Do this in the main activity. Here R.id.full_screen_destination is id of the fragment in navigation fragment.


To access your BottomNavigationView from within the fragments use the following code:

BottomNavigationView navBar = getActivity().findViewById(R.id.bottomBar);

Try this,

Add this line in the BottomNavigationView in Xml

app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"

And Implement this BottomNavigation behavior using CoOrdinator Layout and you can hide or show the view using the scroll listeners.

public class BottomNavigationViewBehavior extends CoordinatorLayout.Behavior<BottomNavigationView> {

private int height;

@Override
public boolean onLayoutChild(CoordinatorLayout parent, BottomNavigationView child, int layoutDirection) {
    height = child.getHeight();
    return super.onLayoutChild(parent, child, layoutDirection);
}

@Override
public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout,
                               BottomNavigationView child, @NonNull 
                               View directTargetChild, @NonNull View target,
                               int axes, int type)
{
    return axes == ViewCompat.SCROLL_AXIS_VERTICAL;
}

@Override
public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull BottomNavigationView child,
           @NonNull View target, int dxConsumed, int dyConsumed,
           int dxUnconsumed, int dyUnconsumed, 
            @ViewCompat.NestedScrollType int type)
{
   if (dyConsumed > 0) {
       slideDown(child);
   } else if (dyConsumed < 0) {
       slideUp(child);
   }
}

private void slideUp(BottomNavigationView child) {
    child.clearAnimation();
    child.animate().translationY(0).setDuration(200);
}

private void slideDown(BottomNavigationView child) {
    child.clearAnimation();
    child.animate().translationY(height).setDuration(200);
}

}

Add this line code to your Activity where it contains bottom navigation

bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_nav);
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) 
bottomNavigationView .getLayoutParams();
layoutParams.setBehavior(new BottomNavigationViewBehavior());

Try this and let me know Digvijay.Happy Coding.


As the fragment is always inside an activity and you can call getActivity() in fragment to access objects that already exist in the activity. So you can do this:

Activity

public class MainActivity extends Activity {
//...
   Toolbar toolbar;
//...
   public Toolbar getNav() {
      return toolbar;
   }
//...
}

Fragment

//...
if(getActivity() != null && getActivity instanceOf MainActivity)
    ((MainActivity)getActivity()).getNav().setVisiblity(View.GONE);
//...