Search code examples
androidandroid-constraintlayoutandroid-cardview

Buttons placement inside CardView


I try to make a View with two card views. In one of them I would like to place two buttons: bottom right ("btn_save") and bottom left ("btn_cancel") but I can't place the bottom right ("btn_save") button. I've tried to align the button with app:layout_constraintEnd_toEndOf="parent" but no success. How can I place these buttons in the correct way?

<?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=".MainActivity">

    <com.google.android.material.card.MaterialCardView

        android:id="@+id/text_card"
        android:layout_width="0sp"
        android:layout_height="0sp"
        android:layout_gravity="fill"
        android:backgroundTint="#eceaf3"
        android:visibility="visible"
        app:cardCornerRadius="10sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHeight_percent="0.7"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.8"
        app:rippleColor="#00c89e"
        tools:ignore="MissingConstraints">

        <--
        Some design elements are here...  
        -->

    </com.google.android.material.card.MaterialCardView>

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/menu_card"
        android:layout_width="0sp"
        android:layout_height="0sp"
        android:layout_gravity="fill"
        android:backgroundTint="#eceaf3"
        android:visibility="visible"
        app:cardCornerRadius="10sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHeight_percent="0.7"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.8"
        app:rippleColor="#00c89e">

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginStart="15sp"
            android:layout_marginBottom="10sp"
            android:text="Cancel" />

           <--
           I have a problem with this button:  
           -->
        <Button
            android:id="@+id/btn_save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginEnd="15sp"
            android:layout_marginBottom="10sp"
            android:text="Save"
            app:layout_constraintEnd_toEndOf="parent" />

    </com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>

Solution

  • MaterialCardView extends FrameLayout, which don't have methods for lying child Views next to each. just put straight inside MaterialCardView some LinearLayout or RelativeLayout and inside of this addtional ViewGroup place your Buttons

    PS. please don't use sp unit in that way, you should use dp. sp is for fonts only