How to set button above CardView ? (Working on the XML but now when App launches)

490 views Asked by At

I can't put a button above a CardView.
It shows above in the XML when I set translationZ for the button, but when I launch the App, the button is still not displayed above.

Here is my XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    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="match_parent"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintEnd_toStartOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:cardCornerRadius="10dp"
        app:cardElevation="12dp">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:src="@drawable/imageDrawable"/>

    </androidx.cardview.widget.CardView>

    <Button
        android:id="@+id/button"
        android:layout_width="38dp"
        android:layout_height="40dp"
        android:elevation="16dp"
        android:translationZ="16dp"
        android:background="@drawable/ic_play"
        app:layout_constraintBottom_toBottomOf="@+id/cardView"
        app:layout_constraintEnd_toEndOf="@+id/cardView"
        app:layout_constraintStart_toStartOf="@+id/cardView"
        app:layout_constraintTop_toTopOf="@+id/cardView" />
</androidx.constraintlayout.widget.ConstraintLayout>

It is a question asked multiple time, but I don't see any reason why this is not working properly.

It is just a play Button above the ImageView.

I also changed position of my button within the XML, putting it in last position, nothing changed.

1

There are 1 answers

0
snachmsm On BEST ANSWER

consider adding Button inside CardView with layout_gravity="center" (CardView extends FrameLayout)