FloatingActionButton, layout_anchor and layout_gravity
Asked Answered
M

2

11

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?

Mick answered 25/5, 2016 at 22:17 Comment(0)
T
19

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" />
Tc answered 17/8, 2016 at 16:28 Comment(0)
C
0

if you use constraint layout, you should set fab's top and bottom constraint to your desired layout edge like this. for example I have a cardview witch I want to anchor fab to it

<androidx.cardview.widget.CardView
    android:id="@+id/mix_video_cardView/>

<com.google.android.material.floatingactionbutton.FloatingActionButto
    app:layout_constraintTop_toBottomOf="@+id/mix_video_cardView"
    app:layout_constraintBottom_toBottomOf="@+id/mix_video_cardView"
    app:layout_constraintStart_toStartOf="@+id/mix_video_cardView"
    app:layout_constraintEnd_toEndOf="@+id/mix_video_cardView"
    />
Calcicole answered 15/9, 2019 at 18:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.