I'm trying to hide my bottom appBar in other fragments, so it cannot be visible. I tried to do it in this way
public void hideBottomMenu() {
appBarLayout.setVisibility(View.GONE);
bottomNavigationView.setVisibility(View.GONE);
fabAdd.hide();
}
Here are some screenshots:
But it doesn't work correctly, in my view, it becomes invisible, but when I'm scrolling some sort of view appears in the bottom of the screen (neither visibility of bottom app bar is gone).
Here is my MainActivity layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="55dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:layout_scrollFlags="scroll|enterAlways"
app:menu="@menu/top_menu"
app:title="Реєстр ТТН" />
<FrameLayout
android:id="@+id/flInternetAbsent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E36363"
android:padding="2dp"
android:visibility="gone">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:letterSpacing="0.05"
android:text="Відсутній інтернет"
android:textColor="@color/white" />
</FrameLayout>
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:fabCradleMargin="10dp"
app:fabCradleRoundedCornerRadius="20dp">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="16dp"
android:background="@android:color/transparent"
app:itemIconTint="@color/bottom_nav_color"
app:itemTextColor="@color/bottom_nav_color"
app:menu="@menu/bottom_nav_menu" />
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/add"
app:backgroundTint="@color/primary_color"
app:layout_anchor="@id/bottomAppBar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
There are many ways for this (that I don't like none of them). I prefer to setup bottom bar inside of fragments because in most fragments I don't use it. So for a handful of fragments this is fine.
The second way is same as your approach. Note that it is important to set visibility in this order (I've achieved this through try and error after days of hopeless approaches.): first hide FAB (using fab.hide()
) then invisible it (I do this just to be sure that it will not appear anymore. using fab.setVisibility(View.GONE)
) , then Invisible BottomBar (using bottomAppBar.setVisibility(View.GONE)
). in Summary:
public void hideBottomMenu() {
fabAdd.hide();
fabAdd.setVisibility(View.GONE);
appBarLayout.setVisibility(View.GONE);
//bottomNavigationView.setVisibility(View.GONE); do not use this. it is a child of bottombar.
}
For showing these components define:
public void showBottomMenu() {
appBarLayout.setVisibility(View.VISIBLE);
appBarLayout.performShow();
//fabAdd.setVisibility(View.VISIBLE); // I am not sure about this.
fabAdd.show();
//bottomNavigationView.setVisibility(View.GONE); do not use this. it is a child of bottombar.
}
Then in onCreate
method of fragments that you want to hide bottomBar, call it as
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((MainActivity) requireActivity()).hideBottomMenu();
}