4

I want to hide my bottom bar on scroll but a FAB should stay on the screen.

If I put the FAB on top of BottomNavigationView using anchors but it appears behind it.

If I put layout_insetEdge="bottom" to the BottomNavigationView then it works but make my tests fail (https://issuetracker.google.com/issues/70162122) so I cannot use that at the moment.

enter image description here

Layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns: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/mainLayout"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true"
  tools:context=".screens.main.MainActivity">

  <FrameLayout
    android:id="@+id/mainContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/scrolling_view_behaviour" />

  <android.support.design.widget.FloatingActionButton
    android:id="@+id/mainNewQuestionButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/spacing_8dp"
    app:layout_anchor="@+id/mainBottomNavigationView"
    app:layout_anchorGravity="top|right|end"
    app:srcCompat="@drawable/create_question"
    tools:visibility="visible" />

  <android.support.design.widget.BottomNavigationView
    android:id="@+id/mainBottomNavigationView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:itemBackground="@color/my_gray"
    app:itemIconTint="@drawable/selector_bottombar_item"
    app:itemTextColor="@drawable/selector_bottombar_item"
    app:layout_anchor="@id/mainContainer"
    app:layout_anchorGravity="bottom"
    app:layout_behavior="@string/hide_bottom_navigation_view_behaviour"
    app:menu="@menu/bottom_navigation_main" />

</android.support.design.widget.CoordinatorLayout>
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Daniel Gomez Rico
  • 15,026
  • 20
  • 92
  • 162

2 Answers2

4

Just use:

<androidx.coordinatorlayout.widget.CoordinatorLayout>

 <com.google.android.material.bottomnavigation.BottomNavigationView
      android:id="@+id/bottom_navigation"
      android:layout_gravity="bottom"
      .../>

  <com.google.android.material.floatingactionbutton.FloatingActionButton
      android:layout_gravity="top"
      app:layout_anchor="@id/bottom_navigation"
      app:layout_anchorGravity="top|end"
      />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
2

You seem to be having a Z-Ordering issue here. You could try the fab.bringToFront() followed by fab.invalidate() for api's below 21, and ViewCompat.setZ(fab, someFloat) for api's above 21.

I used these methods to bring a custom FloatingActionMenu, consisting of a series of FAB's I wrote for a project, to the front so they would overlap everything on the screen.

Azethoth
  • 2,276
  • 1
  • 13
  • 10