javaandroidfloating-action-buttonlayout-anchor

Floating action button layout anchor not working


I want a floating action button in between my two relative layouts.

For this I have taken parent layout as coordinator layout and specified the anchor and anchor gravity to fab button.

But its not getting set where I want it to be.

I want it to set at right corner of relative layout6 at the end and between relative layout6 and relative layout3 on right corner.

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bg"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <RelativeLayout
                android:id="@+id/relativeLayoutParent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/imageView5"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginEnd="30dp"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:layout_marginStart="30dp"
                android:layout_marginTop="30dp"
                android:background="@color/colorAccent">

                <RelativeLayout
                    android:id="@+id/relativeLayout6"
                    android:layout_width="match_parent"
                    android:layout_height="240dp"
                    android:layout_centerHorizontal="true">

                    <ImageView
                        android:id="@+id/imageView7"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentStart="true"
                        android:layout_alignParentTop="true"
                        android:scaleType="fitXY"
                        app:srcCompat="@drawable/profile_img" />

                    </LinearLayout>

                </RelativeLayout>


                <RelativeLayout
                    android:id="@+id/relativeLayout3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentStart="true"
                    android:layout_below="@+id/relativeLayout6">


            </RelativeLayout>


            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_anchorGravity="center"
                app:layout_anchor = "@id/linearLayout"
                android:layout_margin="@dimen/fab_margin"
                app:srcCompat="@android:drawable/ic_dialog_email" />
        </LinearLayout>

    </ScrollView>


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

Please help.. Thank you.


Solution

  • Put FloatingActionButton inside CoordinatorLayout. The anchor property only works inside the CoordinatorLayout.
    One more thing: Your layout is messy. Please arrange it.

    <?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bg"
    android:orientation="vertical">
    
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <RelativeLayout
                android:id="@+id/relativeLayoutParent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/imageView5"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginEnd="30dp"
                android:layout_marginLeft="30dp"
                android:layout_marginRight="30dp"
                android:layout_marginStart="30dp"
                android:layout_marginTop="30dp"
                android:background="@color/colorAccent" />
    
            <RelativeLayout
                android:id="@+id/relativeLayout6"
                android:layout_width="match_parent"
                android:layout_height="240dp"
                android:layout_centerHorizontal="true" />
    
            <ImageView
                android:id="@+id/imageView7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:scaleType="fitXY"
                app:srcCompat="@drawable/profile_img" />
    
        </LinearLayout>
    
    
        <RelativeLayout
            android:id="@+id/relativeLayout3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/relativeLayout6" />
    </ScrollView>
    
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        app:layout_anchor="@id/linearLayout"
        app:layout_anchorGravity="center"
        app:srcCompat="@android:drawable/ic_dialog_email" />