Search code examples
androidandroid-cardview

Cardview in Contraint Layout resizing not as supposed


I'm having trouble with this layout because is not working as I supposed. The textview inside the CardView is afecting the other views and the size of the card.

App sample screenshot

But when I see the xml, the other elements are not constrained to the size of the textview (or maybe I'm just stuck and I can't see it).

This is my xml. At design time, it works fine, but in runtime, the card width resizes. Any ideas?:

<?xml version="1.0" encoding="utf-8"?>
 
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="#C6C6C6"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="5dp"
card_view:cardUseCompatPadding="true">

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">

<ImageView
    android:id="@+id/company_logo"
    android:layout_width="68dp"
    android:layout_height="68dp"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:src="@drawable/aguas_cartagena"
    android:contentDescription="@string/company_logo"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/company_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginStart="8dp"
    android:layout_toEndOf="@+id/company_logo"
    android:text="XXX"
    android:textSize="24sp"
    app:layout_constraintStart_toEndOf="@+id/company_logo"
    app:layout_constraintTop_toTopOf="@+id/company_logo" />

<TextView
    android:id="@+id/pay_date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/company_name"
    android:layout_marginTop="8dp"
    android:layout_toEndOf="@+id/company_logo"
    android:text="Fecha pago: 14-Abr-2020"
    app:layout_constraintStart_toStartOf="@+id/company_name"
    app:layout_constraintTop_toBottomOf="@+id/company_name" />

<ImageView
    android:id="@+id/icon_status"
    android:layout_width="32dp"
    android:layout_height="32dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="@+id/company_name"
    app:srcCompat="@android:drawable/presence_online" />

<TextView
    android:id="@+id/pay_value"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="$ 999.999.999"
    app:layout_constraintEnd_toEndOf="@+id/icon_status"
    app:layout_constraintTop_toBottomOf="@+id/company_name" />
 
</androidx.constraintlayout.widget.ConstraintLayout>
 
</androidx.cardview.widget.CardView>

EDIT: For who has the same problem, I've found a solution in this post (second accepted answer): Use RelativeLayout as the immediate parent to CardView.


Solution

  • For who has the same problem, I've found a solution in this post (second accepted answer): Use RelativeLayout as the immediate parent to CardView

    The solution was to enclose the Cardview layout inside a RelativeLayout:

    <RelativeLayout 
        android:layout_width="match_parent" ... >
        <CardView 
            android:layout_width="match_parent" ... >
        </CardView>
    </RelativeLayout>