Search code examples
androidlayoutmaterial-designfloating-action-button

FAB aligns to AppBarLayout when adding one element to recycler view


I have problem in creating layout based on CoordinatorLayout. Layout is pretty simple it contains Toolbar, RecyclerView, TextView (which replaces recycler view when there is no elements in list) and Floating Action Button. Here is the code :

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/mainCoordinatorLayout"
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="com.steveq.qroclock_20.presentation.activities.MainActivity"
android:orientation="vertical">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize">
    <android.support.v7.widget.Toolbar
        android:id="@+id/mainToolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:elevation="4dp"
        android:fitsSystemWindows="true">
        <TextView
            android:id="@+id/toolbarTitleTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:text="@string/app_name"
            android:textSize="24sp"
            fontPath="fonts/SourceSansPro-Bold.ttf"
            android:textColor="@color/material_teal_800"/>
    </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/alarmsRecyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    android:padding="4dp"
    android:visibility="visible"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<TextView
    android:id="@+id/emptyRecyclerViewReplacement"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:visibility="gone"
    android:text="no alarms" />


<android.support.design.widget.FloatingActionButton
    android:id="@+id/addAlarmFab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/plus_vec"
    android:layout_margin="16dp"
    app:layout_anchor="@id/alarmsRecyclerView"
    app:layout_anchorGravity="bottom|right|end"
    android:clickable="true"

The problem is that when I start the app and add item to recycler view it FAB aligns to the toolbar (AppBarLayout?). FAB goes to its destination when I add another item. Below I add also pictures how it looks. Have you any idea about the cause of this issue ?

Regards, Adam

enter image description here

enter image description here


Solution

  • Your XML layout is not complete, but at first glance, Im not sure you need to set an anchor in your FloatingActionButton. You can try replacing

    app:layout_anchor="@id/alarmsRecyclerView"
    app:layout_anchorGravity="bottom|right|end"
    

    with

    android:layout_gravity="bottom|end"