How do I make android support FloatingActionButton at the bottom right of the screen?

I added FloatingActionButton

to my layout inside RelativeLayout

as follows

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@drawable/ic_ok" />

</RelativeLayout>

      

As you can see, I installed layout_gravity

in bottom|right

, but the location of mine has FloatingActionButton

not changed and is located in the upper left corner.

How can I make mine FloatingActionButton

at the bottom right?

+3


source to share


4 answers


Using CoordinatorLayout:

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

<RelativeLayout
    android:id="@+id/test"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</RelativeLayout>

<android.support.design.widget.FloatingActionButton
    android:layout_margin="10dp"
    android:id="@+id/myFAB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:backgroundTint="#FF0000"
    app:borderWidth="0dp"
    app:elevation="8dp"
    app:layout_anchor="@id/test"
    app:layout_anchorGravity="bottom|right|end" />
</android.support.design.widget.CoordinatorLayout>

      



Note

   app:layout_anchor="@id/test"
   app:layout_anchorGravity="bottom|right|end"

      

+8


source


Since you are wrapping your FloatingActionButton from the RelativeLayout, use the following properties instead:

android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"

      



Hope it helped.

+7


source


Removing from your code:

android:layout_gravity="bottom|right"

Add this line:

app:layout_anchorGravity="bottom|right|end"

0


source


Removal from your code:
android: layout_gravity = "bottom | right"

Add this:
app: layout_anchorGravity = "bottom | right | end"

0


source







All Articles