Floating action button layout anchor not working

6.3k Views Asked by At

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.

3

There are 3 best solutions below

4
On BEST ANSWER

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

0
On

Too many errors in open and closed tags.

  • Just before the Imageview, there's a RelativeLayput but you closed it as Linear layout...fix this
  • Fix that RelaLayout as RelativeLayout just after scrollview
  • Errors in close tag of linear layout
  • Open two Relative layouts, only one is closed.

Also, the key to remember Scrollview can host only one direct child.

I've removed all your errors but as Scrollview can only have one child, it's not working as you want it to be.Try to Fix this. Try constraintLayout with guidelines. You'll find it a better way to anchor something.

<?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">

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

             </RelativeLayout>

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


    </ScrollView>


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

Try this hope it works for you...

<?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/grey_200"
    android:orientation="vertical">

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

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

            </RelativeLayout>

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

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

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="60dp"
            android:layout_height="60dp"
            app:layout_anchorGravity="bottom|right|end"
            app:layout_anchor="@+id/appbar"
            android:layout_margin="10dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            app:srcCompat="@android:drawable/ic_dialog_email" />


<android.support.v4.widget.NestedScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:background="@android:color/white"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:text="Hello world"/>

    </RelativeLayout>
</android.support.v4.widget.NestedScrollView>


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