Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Bottom Sheet with Floating Action Button with Snackbar Issue

So I have a Floating Action Button which is anchored to a bottom sheet layout. I then click a button which sets the state of the Bottom Sheet to STATE_HIDDEN. The bottom sheet hides correctly and a snackbar pops up, the Floating Action Button rises accordingly. The issue is after the Snackbar closes and the Floating Action Button sinks back down: when I set the state of the bottom sheet back to STATE_COLLAPSED the Floating Action Button takes a second or 2 to readjust and anchor back onto the bottom sheet (last screenshot). How do I remove the delay? Thanks :)

Images of Issue

enter image description here

Code for the button:

    mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);

    final Snackbar mySnackbar
            = Snackbar.make(findViewById(R.id.bottom_sheet_page),
            R.string.map_undoremove, Snackbar.LENGTH_LONG);
    mySnackbar.setAction(R.string.undo_string, new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            mWaypoint = mMap.addMarker(new MarkerOptions().position(mWaypoint.getPosition())
                    .title("Test"));
        }
    });

    mySnackbar.show();

Code for the layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/bottom_sheet_page"
tools:context=".Map.MapNavDrawer" >

<!-- Your content -->
<include layout="@layout/activity_maps" />

<!-- Bottom Sheet -->

<LinearLayout
    android:clickable="true"
    android:id="@+id/bottom_sheet"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:background="#FFFFFF"
    android:orientation="vertical"
    android:padding="16dp"
    app:behavior_hideable="true"
    app:layout_behavior="@string/bottom_sheet_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_weight="1"
        >

        <TextView
            android:id="@+id/textViewWaypoint"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/map_waypointtitle"
            android:textStyle="bold"
            android:textColor="@android:color/black" />

        <TextView
            android:id="@+id/textViewLocName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textStyle="italic"
            android:textColor="@android:color/black" />

        <TextView
            android:id="@+id/textViewLocDetails"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:textColor="@android:color/black" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom|end"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button2"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:drawableTop="@drawable/ic_edit_black_24dp"
            android:onClick="selectPlace"
            android:text="@string/map_editwaypoint" />

        <Button
            android:id="@+id/button3"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:drawableTop="@drawable/ic_delete_black_24dp"
            android:onClick="deleteWaypoint"
            android:text="@string/map_removewaypoint" />

        <Button
            android:id="@+id/button4"
            android:layout_width="0dp"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:text="@string/map_sharewaypoint"
            android:drawableTop="@drawable/ic_menu_share"
            style="?android:attr/buttonBarButtonStyle"/>

    </LinearLayout>

</LinearLayout>

<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="@drawable/map_marker_radius"
    android:tint="@android:color/white"
    app:layout_anchor="@id/bottom_sheet"
    app:layout_anchorGravity="top|end"
    tools:ignore="VectorDrawableCompat" />


</android.support.design.widget.CoordinatorLayout>
like image 574
Kerjifire Avatar asked Sep 09 '17 11:09

Kerjifire


1 Answers

Hi I have exactly the same issue and two suggestions:

Option A: let the snackbar floating from the top below toolbar. See this answer here: https://stackoverflow.com/a/31746370/5320905

Option B: increase the elevation of the snackbar and show it on top of FAB like this

final Snackbar mySnackbar
            = Snackbar.make(findViewById(R.id.bottom_sheet_page),
            R.string.map_undoremove, Snackbar.LENGTH_LONG);
View snackbarView = mySnackbar.getView();
snackbarView.setElevation(30);
like image 137
p72b Avatar answered Sep 27 '22 17:09

p72b