1
<?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:id="@+id/activity_login"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorPrimary">

                <android.support.v7.widget.CardView
                    android:id="@+id/card_authentication_container"
                    style="@style/CardView"
                    android:layout_width="300dp"
                    android:layout_height="wrap_content"
                    android:layout_above="@id/button_authentication"
                    android:layout_gravity="center_horizontal|top"
                    android:layout_marginTop="32dp"
                    app:cardCornerRadius="10dp"
                    app:cardElevation="10dp">
                </android.support.v7.widget.CardView>

                <android.support.design.widget.FloatingActionButton
                    android:id="@+id/float_button_authenticate"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/gamepad"
                    app:fabSize="mini"
                    app:layout_anchor="@id/card_authentication_container"
                    app:layout_anchorGravity="bottom|center_horizontal" />
            </android.support.design.widget.CoordinatorLayout>

When I try to set floating action button on CardView with app:layout_anchorGravity="bottom|center_horizontal", it set under CardView.

Like this

André Sousa
  • 1,692
  • 1
  • 12
  • 23
yasser_kh
  • 43
  • 6

1 Answers1

1

The problem is with your:

app:cardElevation="10dp"

Since you CardView elevation will be greater than the one in your FloatingActionButton, it will always appear behind.

Explanation: from the documentation:

Elevation is the relative distance between two surfaces along the z-axis.

So in terms of the z-axis, your CardView is in a much higher place (10dp) than your FAB (since you didn't define any elevation value it will use the default which is 0dp).


Solution: Set an elevation in your FAB with the value 10dp or greater.

Example:

<android.support.design.widget.FloatingActionButton
        android:id="@+id/float_button_authenticate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/gamepad"
        app:elevation="10dp"
        app:fabSize="mini"
        app:layout_anchor="@id/card_authentication_container"
        app:layout_anchorGravity="bottom|center_horizontal" />

Tip: don't use 10dp for your card elevation. Use the recommended value in the material specification which is 1dp.

André Sousa
  • 1,692
  • 1
  • 12
  • 23