Bottom Align Floating Action Button

AndroidFloating Action-Button

Android Problem Overview


I want to align to bottom right my FAB.

  1. I tried with android:gravity="bottom|right"
  2. When I try android:layout_alignParentBottom="true" FAB disappears
  3. When I try android:layout_alignBottom="@id/lista_tiendas" FAB disappears

It doesn't seem complicated but I just can't accomplish it

Any Ideas?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

<ListView
    android:id="@+id/lista_tiendas"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:divider="@android:color/transparent"
    android:dividerHeight="4.0sp"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_add_white_48dp"
    app:backgroundTint="@color/spg_rosa"
    app:borderWidth="0dp"
    app:elevation="8dp"
    app:fabSize="normal"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="@id/lista_tiendas"
     />
</RelativeLayout>

Android Solutions


Solution 1 - Android

For RelativeLayout:

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    ... />

For CoordinatorLayout, you should use android:layout_gravity="end|bottom"


For ConstraintLayout:

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    ... />

See this answer for more information.

Solution 2 - Android

Layout should be RelativeLayout. Try the next code:

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

<LinearLayout
    android:id="@+id/ly_list_form"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">


    <ListView
        android:id="@+id/list_forms"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="1dp" />

</LinearLayout>

<LinearLayout
    android:id="@+id/ly_bar_bottom"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="right"
    android:orientation="horizontal">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/button_addc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="28dp"
        android:src="@drawable/ic_add"
        app:borderWidth="0dp"
        app:elevation="6dp"
        app:pressedTranslationZ="12dp" />
</LinearLayout>

Solution 3 - Android

It appears that FloatingActionButton isn't positioned correctly inside a RelativeLayout.

I changed RelativeLayout to FrameLayout, and problem solved! Hope it helps!

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="#CCC">

<ListView
    android:id="@+id/mylist"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:divider="@android:color/transparent"
    android:dividerHeight="1.0sp" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right|bottom"
    android:src="@drawable/ic_add_white_48dp"
    app:backgroundTint="@color/spg_rosa"
    app:borderWidth="0dp"
    app:elevation="8dp"
    app:fabSize="normal" />

</FrameLayout>

Solution 4 - Android

Try using android.support.design.widget.CoordinatorLayout and android:layout_gravity="bottom|right" works for me.

Solution 5 - Android

I was able to get the code to work by doing this:

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:backgroundTint="#ff0000"
    app:borderWidth="0dp"
    app:elevation="8dp"
    app:fabSize="normal"
    android:layout_alignLeft="@+id/text"
    android:layout_above="@+id/text"/>

However, it didn't work with below the TextView instead of above, I believe it could be a bug.

Solution 6 - Android

Try

> android:layout_gravity="bottom|right"

Solution 7 - Android

To Align The Button At Bottom and center of the Relative Layout

Add these two in your floatingactionbutton container

    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"

and the output will be: like this

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJuliatzinView Question on Stackoverflow
Solution 1 - AndroidAnggrayudi HView Answer on Stackoverflow
Solution 2 - AndroidDiegoView Answer on Stackoverflow
Solution 3 - AndroidJuliatzinView Answer on Stackoverflow
Solution 4 - AndroidnaamadheyaView Answer on Stackoverflow
Solution 5 - AndroidBobbyView Answer on Stackoverflow
Solution 6 - AndroidSachini SamarasingheView Answer on Stackoverflow
Solution 7 - AndroidNabil NazarView Answer on Stackoverflow