Search code examples
androidxmlandroid-layoutfloating-action-button

Floating Action Button Refusing to anchor to Action Bar


I have a floating action button inside my activity_main layout. I want it to be anchored to the bottom right of the action bar. I have implemented the normal steps of anchoring a button to the action bar but for some reason it is not working. All I get is this Error Fab

As you can see in the picture, the fab isn't overlapping Action Bar for some reason

Here is my layout code

activity_main.xml

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:src="@drawable/head"
            android:id="@+id/img"/>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="28dp"
            android:layout_marginBottom="40dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Pending Work"
                android:textColor="#ffffff"
                android:textSize="35sp" />

            <TextView
                android:id="@+id/num"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0 Items"
                android:textColor="#ffffff"
                android:textSize="25sp"
                android:layout_marginBottom="20dp"/>
        </LinearLayout>
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@null"
            app:popupTheme="@style/AppTheme.PopupOverlay" />


    </RelativeLayout>

 </android.support.design.widget.AppBarLayout>

<include
    android:id="@+id/include"
    layout="@layout/content_main" />

 <android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    app:elevation="8dp"
    app:fabSize="normal"
    app:layout_anchor="@+id/appBar"
    app:layout_anchorGravity="right|bottom"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    android:tint="#fff"
    app:srcCompat="@android:drawable/ic_input_add" />

</android.support.design.widget.CoordinatorLayout>

I don't know what is wrong with the code. Please help me.


Solution

  • Remove the layout_gravity attribute, try this:

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        app:elevation="8dp"
        app:fabSize="normal"
        app:layout_anchor="@id/appBar"
        app:layout_anchorGravity="bottom|end"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:tint="#fff"
        app:srcCompat="@android:drawable/ic_dialog_email"
        />