Search code examples
androidxmlandroid-recyclerviewfloating-action-buttonandroid-coordinatorlayout

Issue with CoordinatorLayout and FloatingActionButton


On picture 1 it looks fine but on picture 2, because FAB has app:layout_anchor="@id/recyclerview_list" attribute, it looks not fine. Is there a way to anchor FAB to the bottom side without adding custom bottom nav bar? I just want this button to stay on position permanently even if the RecyclerView changes it's height.

Here's the XML file:

<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/main_layout"
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="match_parent"
tools:context="fragments.DailyListFragment">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/appbar_layout"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|snap|exitUntilCollapsed"
        app:title="@string/sample"
        app:contentScrim="@color/accent_color1">

        <ProgressBar
            android:id="@+id/progressBar2"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:progressTint="#BA0E0E" />

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"/>

    </com.google.android.material.appbar.CollapsingToolbarLayout>

</com.google.android.material.appbar.AppBarLayout>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab_new_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="24dp"
    android:layout_marginBottom="24dp"
    android:src="@drawable/ic_add_black_24dp"
    android:visibility="visible"
    app:fabSize="normal"
    app:layout_anchor="@id/recyclerview_list"
    app:layout_anchorGravity="bottom|right|end"/>

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerview_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:alwaysDrawnWithCache="false"
    android:background="@color/colorBackground"
    android:visibility="visible"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:listitem="@layout/recyclerview_layout_list">

</androidx.recyclerview.widget.RecyclerView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Solution

  • Replace

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        app:layout_anchor="@id/recyclerview_list" 
        app:layout_anchorGravity="bottom|right|end"
    

    with

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:layout_gravity="bottom|end"