Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

xml

I have added a FloatingActionButton to my layout inside a RelativeLayout as follow

<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 see, I have set layout_gravity to bottom|right but the place of my FloatingActionButton hasn't changed and it rests on top left.

How can i make my FloatingActionButton at bottom right?

like image 641
MChaker Avatar asked Dec 03 '22 16:12

MChaker


1 Answers

Using the 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"
like image 55
Blackbelt Avatar answered Dec 28 '22 11:12

Blackbelt