Search code examples
androidfloating-action-button

FloatingActionButton anchor on top of a View


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 .


Solution

  • 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" />