james04
james04

Reputation: 1920

ConstraintLayout set percentage to imageview width and height in RecyclerView item

When we have a recyclerView with images i want to have the image (from which i dont know the dimentions of it) to have a max width and a max height as a percentage of the screen

<?xml version="1.0" encoding="utf-8"?>
<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">

    <ImageView
        android:id="@+id/img_rcv_msg_image"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/img_guideline2"
        app:layout_constraintHorizontal_bias="0.0"
        android:scaleType="centerInside"
        android:background="@drawable/image_received_placeholder"
        app:layout_constraintStart_toStartOf="@+id/guideline6"
        app:layout_constraintTop_toBottomOf="parent"
        app:srcCompat="@android:drawable/ic_menu_report_image"
        tools:ignore="ContentDescription" />

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

<androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="8dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

So i have constrainted the width to be between the two guidelines on the horizontal direction. How can i do this in the vertical also?

Upvotes: 7

Views: 4379

Answers (2)

cutiko
cutiko

Reputation: 10527

In this case you can now the width, because of the guidelines, but you cant do the same for heigth because you cant know if the height of the screen will be proportional on every device.

So you want to keep the ratio between width and height, use:

app:layout_constraintDimensionRatio="1:1"

Play wity the 1:1 proportion and then you will be able to adjust it and should scale to each screen size accordingly.

Upvotes: 2

Antonis Radz
Antonis Radz

Reputation: 3097

try app:layout_constraintHeight_percent="0.2" or app:layout_constraintDimensionRatio="1:1"

Upvotes: 7

Related Questions