1

I want the image in the layout below to scale with different screen sizes. It must maintain an aspect ratio of 1:1, but not grow larger than 150dp

enter image description here

I tried using a combination of Width_max, DimensionRatio & Width_percent, but the image either respects the width percent or shrinks to the combined height of the two adjacent text views

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="4dp"
    android:background="@android:color/white"
    app:cardCornerRadius="4dp"
    app:cardElevation="@dimen/cardElevation"
    app:cardUseCompatPadding="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/thumbnailImageView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:background="@android:color/darker_gray"
            android:scaleType="centerCrop"
            app:layout_constraintHorizontal_chainStyle="packed"
            app:layout_constraintHorizontal_bias="0"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@id/titleTextView"
            app:layout_constraintWidth_max="150dp"
            app:layout_constraintDimensionRatio="1:1"
            app:layout_constraintWidth_percent="0.2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/titleTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="8dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="@string/placeholder"
            android:textColor="@android:color/black"
            android:textSize="@dimen/textMedium"
            app:layout_constraintBottom_toTopOf="@id/descriptionTextView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/thumbnailImageView"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_chainStyle="packed" />

        <TextView
            android:id="@+id/descriptionTextView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="2"
            android:text="@string/placeholder"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@id/titleTextView"
            app:layout_constraintStart_toStartOf="@id/titleTextView"
            app:layout_constraintTop_toBottomOf="@id/titleTextView" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.cardview.widget.CardView>
aneurinc
  • 1,238
  • 12
  • 20
  • https://stackoverflow.com/a/61478206/12478830 – MMG Jul 11 '20 at 04:47
  • not sure. just give it a shot. `android:layout_width="150dp"` `app:layout_constraintWidth_percent="0.2"` `app:layout_constraintWidth_max="wrap"` `app:layout_constraintDimensionRatio="1:1"` – momvart Jul 11 '20 at 05:33
  • @MohammadOmidvar Unfortunately that didn't work. – aneurinc Jul 11 '20 at 18:29

1 Answers1

2

This is a strange behaviour by ConstraintLayout

But I have found an alternative to make the dimensions follow properly using Guideline

Adding a Guideline at 20 percent width in the ConstraintLayout

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent=".2" />

Make layout_constraintEnd_toStartOf of the ImageView point to the guideline

<ImageView
    ...
    app:layout_constraintEnd_toStartOf="@id/guideline2"
    ... />

Remove the app:layout_constraintWidth_percent="0.2" from the ImageView

Leaving the layout_constraintWidth_max of the ImageView at 150dp will constraint it to a maximum of 150dp but if the guideline comes before 150dp it follow the guideline as a constraint.

Manav Jain
  • 1,406
  • 1
  • 8
  • 5