0

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.

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

3 Comments

Even with this change, I am not seeing the image but let' say if I put this in image view, I see the image android:layout_width="104dp" android:layout_height="162dp"
maybe thats because of layout_width and layout_height set to wrap content, when there is no content at the beginning. also scaleType="fitXY" is opposite to width and height - how to fix XY when there is no XY, it depends on image (and its size should fit XY, looped)
sadly without wider context, some reproducible example, there may be much guessing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.