How to overlap the card with a disabled FAB?

Viewed 69

I have a layout with a CardView and a disabled ExtendedFloatingActionButton which I set enabled programmatically, when that FAB is enabled all works fine, but when I set it to disabled it goes behind the card.

I've tried to set the elevation to both card and FAB by setting a higher elevation to the FAB but when it's disabled it anyway goes above..

Here is how it looks like:

enter image description here

And I would show it overlapped like when enabled is set to true:

enter image description here

Here is my code:

<androidx.constraintlayout.widget.ConstraintLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
        android:id="@+id/fabInvia"
        android:layout_width="wrap_content"
        android:enabled="false"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:contentDescription="@string/invia"
        android:text="@string/invia"
        android:textColor="#fff"
        app:elevation="10dp"
        android:translationY="-33dp"
        app:fabSize="normal"
        app:icon="@drawable/ic_baseline_send"

        app:iconTint="#fff"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/cardView" />

 <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardElevation="5dp"
        app:elevation="5dp"
        app:cardUseCompatPadding="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

    ...

    </androidx.cardview.widget.CardView>


</androidx.constraintlayout.widget.ConstraintLayout>
2 Answers

The issue isn't exclusive to a disabled ExtendedFloatingActionButton, any other view will behave the same.

Reason:

Because the CardView has a special elevation attribute app:cardElevation that can only be applied to CardViews, so translation-z or app:elevation attributes won't solve this issue.

Solution:

Wrapping the ExtendedFloatingActionButton into another CardView. But some effort is needed to make this CardView like it doesn't exist, to do that you need to:

  • Set exactly the same app:cardElevation on both CardViews (This actually tackles the elevation issue you have.
  • Set a transparent background color to the new CardView, app:cardBackgroundColor="@android:color/transparent" doesn't work for some reason, but doing this programmatically tackle it:
cardView2.setBackgroundColor(ContextCompat.getColor(this, android.R.color.transparent));

Also transfer the constraints/margin from the ExtendedFloatingActionButton to the outer CardView

Here is a sample

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:cardElevation="5dp"
        app:cardUseCompatPadding="true"
        app:elevation="5dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        app:cardElevation="5dp"
        app:layout_constraintBottom_toBottomOf="@+id/cardView"
        app:layout_constraintEnd_toEndOf="parent">

        <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
            android:id="@+id/fabInvia"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:enabled="false"
            android:gravity="center"
            android:text="@string/invia"
            android:textColor="#fff"
            app:fabSize="normal"
            app:iconTint="#fff" />

    </androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

You could try:

  • To swap the order in the XML (cardview first, then FAB)
  • Changing the translation-z values
  • Placing the fab in a separate frame layout on top of the constraint layout
Related