Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Floating Action Button gravity not working

I have a floating action button which should be in the bottom right corner, but apparently the gravity isn't working. It is shown in the top left corner. Here is my code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/rl"
            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">

<ImageView
    android:id="@+id/iv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="16dp"
    android:src="@mipmap/ic_action_send"
    app:backgroundTint="@color/colorPrimaryDark"
    app:layout_anchor="@id/iv"
    app:layout_anchorGravity="bottom|right|end"/>

</RelativeLayout>
like image 542
Kæmpe Klunker Avatar asked Nov 30 '25 11:11

Kæmpe Klunker


1 Answers

I have a floating action button which should be in the bottom right corner, but apparently the gravity isn't working

it is the expected behavior unless you wrap your content around a CoordinatorLayout. E.g

<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:fitsSystemWindows="true">


     <ImageView
      android:id="@+id/iv"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>

 <android.support.design.widget.FloatingActionButton
       android:id="@+id/fab"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_gravity="bottom|end"
       android:layout_margin="16dp"
       android:src="@mipmap/ic_action_send"
       app:backgroundTint="@color/colorPrimaryDark"
       app:layout_anchor="@id/iv"
       app:layout_anchorGravity="bottom|right|end"/>
 </android.support.design.widget.CoordinatorLayout>

should do it. You can read more about the CoordinatorLayout here

like image 55
Blackbelt Avatar answered Dec 02 '25 00:12

Blackbelt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!