Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 849
Sid Avatar asked Jul 26 '17 12:07

Sid


People also ask

How do you adjust a floating button?

Add the floating action button to your layoutThe size of the FAB, using the app:fabSize attribute or the setSize() method. The ripple color of the FAB, using the app:rippleColor attribute or the setRippleColor() method. The FAB icon, using the android:src attribute or the setImageDrawable() method.

What is Layout_anchor?

app:layout_anchor: This attribute can be set on children of the CoordinatorLayout to attach them to another view. The value would be the id of an anchor view that this view should position relative to. Note that, the anchor view can be any child View (a child of a child of a child of a CoordinatorLayout, for example).


1 Answers

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

like image 150
nhp Avatar answered Nov 14 '22 22:11

nhp