2

i want to anchor FloatingActionButton on top of the view ,
and this is my code :

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".ui.activity.MainActivity">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <com.alirezaafkar.toolbar.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:direction="rtl"
                app:navigationIcon="@drawable/ic_arrow_back_white_24dp"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:title="@string/app_name">

                <TextView
                    android:id="@+id/tv_toolbar_title"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:textColor="@android:color/white"
                    android:textSize="20dp"
                    android:textStyle="bold" />

            </com.alirezaafkar.toolbar.Toolbar>

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

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

            <FrameLayout
                android:id="@+id/container_body"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        </FrameLayout>

        <ViewSwitcher
            android:id="@+id/viewSwitcherBottomBar"
            android:layout_width="wrap_content"
            android:layout_height="120dp"
            android:layout_gravity="bottom"
            android:inAnimation="@android:anim/slide_in_left">

            <include layout="@layout/bottom_bar_dest" />

            <include layout="@layout/bottom_bar_request" />


        </ViewSwitcher>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab_my_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:layout_margin="16dp"
            android:layout_marginBottom="60dp"
            android:paddingBottom="60dp"
            android:src="@drawable/ic_my_location_white_24dp"
            app:borderWidth="0dp"
            app:elevation="6dp"
            app:fabSize="mini"
            app:layout_anchor="@id/viewSwitcherBottomBar"
            app:layout_anchorGravity="top|right" />


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


and this is the result ,
but the result is not exactly at the top of view :

enter image description here


how can i bring floatingActionButton exactly on top of the view ?
edit :
i want the floatingActionButton to have no overlaping with anchored view .

ReZa
  • 1,273
  • 2
  • 18
  • 33

2 Answers2

2

instead of giving padding and margin to FloatingActionButton , i set the padding in the view and it works :

    <ViewSwitcher
        android:id="@+id/viewSwitcherBottomBar"
        android:layout_width="wrap_content"
        android:layout_height="120dp"
        android:paddingTop="25dp"
        android:layout_gravity="bottom"
        android:inAnimation="@android:anim/slide_in_left">

        <include layout="@layout/bottom_bar_dest" />

        <include layout="@layout/bottom_bar_request" />

    </ViewSwitcher>


<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_my_location"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:src="@drawable/ic_my_location_white_24dp"
    app:borderWidth="0dp"
    app:elevation="6dp"
    app:fabSize="mini"
    app:layout_anchor="@id/viewSwitcherBottomBar"
    app:layout_anchorGravity="top|right" />
ReZa
  • 1,273
  • 2
  • 18
  • 33
0

Below three lines are essential:

android:layout_gravity="top"
app:layout_anchor="@+id/layout_store_detail"
app:layout_anchorGravity="top|end"

The whole XML looks 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <!-- This is the bottom sheet which pops up -->
    <LinearLayout
        android:id="@+id/layout_store_detail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="vertical"
        app:behavior_hideable="true"
        app:behavior_peekHeight="0dp"
        app:behavior_skipCollapsed="true"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
        >
        ...
    </LinearLayout>


    <!-- This is the Floating action button with a margin of 16dp -->
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        app:layout_anchor="@+id/layout_store_detail"
        app:layout_anchorGravity="top|end"
        >
        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab_locate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:src="@drawable/globe"
            />
    </FrameLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Chandler
  • 3,061
  • 3
  • 22
  • 30