Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FAB from design support library does not move upward when snackbar is shown

This post says:

When you add a FloatingActionButton as a child of your CoordinatorLayout and then pass that CoordinatorLayout to your Snackbar.make() call - instead of the snackbar displaying over the floating action button, the FloatingActionButton ... automatically move upward as the snackbar animates in and returns to its position when the snackbar animates out

I've made exactly as described there but FAB does not move upward. (Snackbar can be swiped out, it means that CoordinatorLayout knows about it.)

Upd

Layout:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/action_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:theme="@style/ThemeOverlay.ActionBar"/>

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/action_toolbar"/>

    </RelativeLayout>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:layout_marginTop="?attr/actionBarSize"
        android:background="@color/theme_primary_darker_color"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"/>

</android.support.v4.widget.DrawerLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/toolbar_action_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_navigation_white_24dp"
    android:layout_margin="16dp"
    android:visibility="gone"
    app:borderWidth="0dp"
    app:elevation="6dp"
    app:layout_anchor="@id/action_toolbar"
    app:layout_anchorGravity="bottom|right|end"
    app:pressedTranslationZ="12dp"/>

</android.support.design.widget.CoordinatorLayout>

Code:

mCoordinatorLayout = (CoordinatorLayout) findViewById(R.id.main_content);
...
Snackbar.make(mCoordinatorLayout, R.string.waypoint_deleted, Snackbar.LENGTH_LONG).show();
like image 945
Andrey Novikov Avatar asked Feb 10 '23 06:02

Andrey Novikov


1 Answers

I've written sample project based on the code you posted and I had to change three things to make FloatingActionButton aware of the Snackbar.

  1. Set 'minifyEnabled' property of my build to false - looks like Proguard strips out some annotations (e.g. Behaviours) which are necessary to coordinate FloatingActionButton. You'll need to add some Proguard rules in your release build where Proguard is enabled.
  2. Pass the FloatingActionButton as a first argument of Snackbar.make(), not the CoordinatorLayout itself as mentioned here.
  3. Remove app:layout_anchor="@id/action_toolbar" and replace app:layout_anchorGravity="bottom|right|end" with the app:layout_gravity="bottom|right|end" from the layout (not sure if it's suitable for you).

If it doesn't work for you, you can try to write your own Behavior class as described in the article mentioned above.

like image 167
aga Avatar answered Apr 30 '23 17:04

aga