androidandroid-layoutfloating-action-buttonlayout-gravitylayout-anchor

FloatingActionButton, layout_anchor and layout_gravity


A bit of a newbie here. I'm about two months into Android development, but I have years of development experience in other environments.

Okay. I have a FloatingActionButton which was not showing up where I expected it or wanted it. It's inside a CoordinatorLayout, along with an AppBarLayout/Toolbar, and following a ListView.

Here is the layout:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.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/fragment_coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".ViewVehicleList">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:title="Vehicle List"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

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

    <ListView
        android:id="@+id/Vehicle_ListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:background="#FFFFFF"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

    </ListView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_AddVehicle"
        style="@style/FloatingAddButton"
        android:src="@drawable/ic_green_add"
        android:layout_gravity="bottom|end"
        app:layout_anchor="@id/Vehicle_ListView"
        android:onClick="addVehicle"/>

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

With this layout, the screen looks like this: FAB in wrong position

My layout_gravity says "bottom|end". I changed it to "bottom|right", but still I got the same result. I've read many tutorials, and researched through Stack Overflow, and have had no luck.

I managed to solve it by removing the anchor listed in the FAB element app:layout_anchor="@id/Vehicle_ListView", which seems to run a counter to what I've read: to use a FAB and position it properly you need to use layout_anchor and layout_gravity. Without the anchor tag, it looks like this:

FAB in correct position

So here's my question: Why is my anchor screwing up the positioning of my FloatingActionButton? What am I doing wrong?


Solution

  • You just need to add layout_anchorGravity.

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_AddVehicle"
        style="@style/FloatingAddButton"
        android:src="@drawable/ic_green_add"
        android:onClick="addVehicle"
        app:layout_anchor="@id/Vehicle_ListView"
        app:layout_anchorGravity="bottom|end" />