MXC
MXC

Reputation: 700

Cannot display an image in Constraint Layout

I have this xml but the image is only shown when I hard the width and height of the image. I am not sure why the constraints as I provided in the code are not working.

<merge
    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="wrap_content"
    android:layout_height="wrap_content"
    tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="4dp"
        android:layout_marginStart="4dp"
        android:layout_marginTop="4dp"
        app:cardCornerRadius="4dp"
        app:cardElevation="2dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/cover"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="fitXY"
            android:importantForAccessibility="no"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:src="@drawable/placeholder" />
    </androidx.cardview.widget.CardView>

</merge>

Note: I cannot change it to compose and this is part of some legacy code.

Upvotes: 0

Views: 48

Answers (1)

snachmsm
snachmsm

Reputation: 19253

note that this part of <ImageView tag is no-op (you can safely remove these lines)

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"

as these attributes are read only by parenting ConstraintLayout, which isn't true, it's CardView for sure... and parent of CardView, well, this depends on place of inflating, maybe CardView should have such params (so also bottom and end constraints), which will/should work when hosted by ConstaintLayout strictly

Upvotes: 0

Related Questions