Search code examples
androidandroid-cardview

Android - Can't set view to match_parent height inside card for recycler view


I have a recycler view which is fed with a card_view that has a color bar (a view) on the left edge which is supposed to occupy the whole height of the card.

I tried setting the height of the color view to match_parent but if I do it like this, the color never appears. If I instead assign a number to the height then it works, but I need the view to adjust to the card's height.

Here is the XML for the card that will feed the recycler view:

<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="wrap_content">

<androidx.cardview.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" >


    <RelativeLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingEnd="14dp"
         >

        <View
            android:id="@+id/swatch"
            android:layout_width="12dp"
            android:layout_height="match_parent" />

        <TextView
            android:id="@+id/pathTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@id/swatch"
            android:layout_marginStart="8dp"
            android:layout_marginTop="14dp"
            android:padding="2dp"
            android:text="@string/placeholder_title"
            android:textColor="@android:color/black"
            android:textSize="14sp" />

        <TextView
            android:id="@+id/pathDescription"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/pathTitle"
            android:layout_toEndOf="@id/swatch"
            android:layout_marginStart="8dp"
            android:layout_marginBottom="14dp"
            android:padding="2dp"
            android:text="@string/placeholder_title"
            android:textColor="@android:color/black"
            android:textSize="14sp" />

    </RelativeLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>

How can I get the "swatch" view to adjust to the height of the card?


Solution

  • Hope it will solve your problem

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <View
                android:id="@+id/swatch"
                android:layout_width="12dp"
                android:layout_height="match_parent" />
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <TextView
                    android:id="@+id/pathTitle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="14dp"
                    android:padding="2dp"
                    android:text="@string/placeholder_title"
                    android:textColor="@android:color/black"
                    android:textSize="14sp" />
                <TextView
                    android:id="@+id/pathDescription"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginBottom="14dp"
                    android:padding="2dp"
                    android:text="@string/placeholder_title"
                    android:textColor="@android:color/black"
                    android:textSize="14sp" />
            </LinearLayout>
        </LinearLayout>
    </androidx.cardview.widget.CardView>