Android - Cannot make FloatingActionButton move up when snackbar is displayed when Fab has a layout_behavior set

44 Views Asked by At

In my app, I have a screen with a fab inside a coordinator layout. When a snackbar is displayed, the fab moves up as the snackbar appears (which is what I expected). However, on another screen, my fab is also inside a coordinator, but on this layoyt I have a recyclerview taking up the full screen size, so I set this attribute app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior" on my fab to mak it hide and appear as the user scroll through the recycler, but the problem is that now, when a snackbar appears, it is showing above the fab (on depth), hidding it.

My code is like this:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".presentation.features.lead.list.LeadsListFragment">

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

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_leads"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layoutAnimation="@anim/slide_in_animation"
            android:orientation="vertical"
            android:visibility="gone"
            tools:itemCount="6"
            tools:listitem="@layout/item_lead" />

    </FrameLayout>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/add_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        android:contentDescription="@string/fab_add_lead_desc"
        android:scaleX="0"
        android:scaleY="0"
        android:visibility="invisible"
        app:backgroundTint="@color/colorPrimary"
        app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
        app:srcCompat="@drawable/ic_add"
        app:tint="@android:color/white" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

I display my Snack like this:

I manage to change my code a bit, and made the snackbar to appear above the fab (on the Y axis). It's slightly better, but still is not the animation I wanted (I want to keep both animations).

Is there a way to do something like this?

I also tried to set an anchorview on my fab, but it still didn't give me what I wanted, because the fab is now on the top left corner of the screen:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".presentation.features.lead.list.LeadsListFragment">

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

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_leads"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layoutAnimation="@anim/slide_in_animation"
            android:orientation="vertical"
            android:visibility="gone"
            tools:itemCount="6"
            tools:listitem="@layout/item_lead" />

        <ProgressBar
            android:id="@+id/spinner_recycler"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:indeterminateTint="@color/link"
            android:indeterminateTintMode="src_atop" />

        <TextView
            android:id="@+id/message_error"
            style="@style/TextAppearance.RecyclerViewMessage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginHorizontal="24dp"
            android:text="@string/message_internal_error"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/message_empty_list"
            style="@style/TextAppearance.RecyclerViewMessage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginHorizontal="24dp"
            android:text="@string/message_no_lead_added"
            android:visibility="gone"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/add_fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:contentDescription="@string/fab_add_lead_desc"
            android:scaleX="0"
            android:scaleY="0"
            android:visibility="invisible"
            app:backgroundTint="@color/colorPrimary"
            app:layout_anchor="@id/content_view"
            app:layout_anchorGravity="bottom|right"
            app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
            app:srcCompat="@drawable/ic_add"
            app:tint="@android:color/white" />

    </FrameLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
Snackbar.make(binding.addFab, R.string.permission_needed, Snackbar.LENGTH_LONG).apply { anchorview = binding.contentView }.show()

The version of the material library I'm using is the 1.8.0:

implementation 'com.google.android.material:material:1.8.0'
0

There are 0 best solutions below