Home » Android » Android Cannot anchor FAB button to BottomSheet

Android Cannot anchor FAB button to BottomSheet

Posted by: admin June 16, 2020 Leave a comment

Questions:

I’m trying to attach a FAB button to the BottomSheet view like Google Maps does. However I cannot make it work,

this is the code of the FAB button:

 <android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email"
    app:layout_anchor="@+id/nestedscrollview" --> BottomSheet view ID
    app:layout_anchorGravity="bottom|end"
    app:elevation="4dp"
    />

and this is the code of the BottomSheet View

<android.support.v4.widget.NestedScrollView
android:id="@+id/nestedscrollview"
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:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
tools:context="com.pub.uac.ui.location.LocationActivity"
android:background="@color/background_white_trasnparent"
 > 
... 
</...
How to&Answers:

It depends on what you mean by “cannot make it work” and also what you try to achieve.

Does it work better if you set on the FAB:

  app:layout_anchorGravity="top|end"

You used “bottom” instead of “top”, so the FAB will not be pushed by the bottom sheet, it will stay at the bottom. With “top” it should at least follow the sheet.