Search code examples
javaandroidfloating-action-buttonlayout-anchor

Floating action button layout anchor not working in my code


the app:layout_anchor not working

I want a floating action button in between my two relative layouts.

For this I have taken parent layout as coordinator layout and specified the anchor and anchor gravity to fab button.

But its not getting set where I want it to be.

i want to put the fab on the middle of the Bottom Navigation Bar

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".Home">

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bottomAppBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="bottom"
        app:fabCradleMargin="10dp"
        app:fabCradleRoundedCornerRadius="10dp"
        app:fabCradleVerticalOffset="10dp"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="674dp"
        tools:ignore="BottomAppBar,MissingConstraints">

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/bottomNavigationView"
            android:layout_marginRight="16dp"
            app:menu="@menu/bottom_nav_menu"
            android:background="@drawable/transparent_background" >
        </com.google.android.material.bottomnavigation.BottomNavigationView>

    </com.google.android.material.bottomappbar.BottomAppBar>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/baseline_add_24"
        app:layout_anchor="@id/bottomAppBar"
        tools:ignore="MissingConstraints"
        />

</androidx.constraintlayout.widget.ConstraintLayout>


**i try to change the gradle material version but that doesn't work **


Solution

  • You have used androidx.constraintlayout.widget.ConstraintLayout and that's why app:layout_anchor is not working . Kindly replace androidx.constraintlayout.widget.ConstraintLayout with Coordinatorlayout

    For more details , visit this link and example

    https://developer.android.com/jetpack/androidx/releases/coordinatorlayout

    FloatingActionButton, layout_anchor and layout_gravity