I’m using the single activity multi fragments with navigation component.how do i hide the bottom navigation bar for some of the fragments?
i tried the following:
- controlling the visibility of the bottomnavigation bar through databinding.(buggy)
- toggling the bottomnavigation visibility before opening the fragment and on the backstack ( buggy)
- making 2 host fragments: 1 full screen, 1 bound by the bottomnavigation
- making 2 navgraphs ..
activity_main.xml:
JavaScript
x
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigation"
android:visibility="@{viewModel.uiUtils.shouldShow ? View.VISIBLE:View.GONE}"/>
mainactivity.java:
JavaScript
private void observeShouldShow() {
mainViewModel.uiUtils.getShouldShow().observe(this, new Observer<Boolean>() {
@Override
public void onChanged(Boolean aBoolean) {
ViewGroup.LayoutParams layoutParams = binding.bottomNavigation.getLayoutParams();
if (mainViewModel.getUiUtils().getShouldShow().getValue()) {
binding.bottomNavigation.setVisibility(View.VISIBLE);
layoutParams.height = 170;
binding.bottomNavigation.setLayoutParams(layoutParams);
} else {
layoutParams.height = 0;
binding.bottomNavigation.setLayoutParams(layoutParams);
binding.bottomNavigation.setVisibility(View.INVISIBLE);
}
}
});
the bottomnavbar blinks when switching between full screen fragments and normal fragments
Advertisement
Answer
i used OnDestinationChangedListener, as @Lavepe answered … sorry didnt check here for a long time here is my code:
JavaScript
if (destinationLabel.equals("FragmentX")) {
showBottomNav(true);
badgeBehaviour(false, false);}
the ui function:
JavaScript
private void showBottomNav(boolean b) {
binding.bottomNavigation.setVisibility(b ? View.VISIBLE : View.GONE);
}
the view above it is :
JavaScript
<fragment
android:id="@+id/navHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@+id/viewcartbadge"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph" />
Best Regards Hope You Find it Useful