I
I
Ilya Kolpakov2021-09-29 17:47:25
Android
Ilya Kolpakov, 2021-09-29 17:47:25

How to make an ImageView that is clipped to the parent view?

I need to create elements for RecyclerView with rounded edges. I made this shape:

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/grey" />
    <corners android:radius="15dp" />
</shape>


Should get this result

61547c6c3cbf9263558241.png

and set it as background in list_item_character

<androidx.constraintlayout.widget.ConstraintLayout
    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_marginTop="25dp"
    android:layout_marginLeft="25dp"
    android:layout_marginRight="25dp"
    android:background="@drawable/rounded_shape_bg">

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="10dp"
        android:text="Character Name"
        android:textColor="@color/white"
        android:textSize="20sp"
        app:layout_constraintStart_toEndOf="@+id/characterPortrait"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvStatusAndRace"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dead - Human"
        android:textColor="@color/white"
        app:layout_constraintStart_toStartOf="@+id/tvName"
        app:layout_constraintTop_toBottomOf="@+id/tvName" />

    <TextView
        android:id="@+id/tvLocation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Last known location:"
        android:layout_marginTop="5dp"
        android:textColor="@color/light_grey"
        app:layout_constraintStart_toStartOf="@+id/tvStatusAndRace"
        app:layout_constraintTop_toBottomOf="@+id/tvStatusAndRace" />

    <TextView
        android:id="@+id/tvLocationInput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Citadel of Ricks"
        android:ellipsize="end"
        android:maxLines="1"
        android:textSize="16sp"
        android:textColor="@color/white"
        app:layout_constraintStart_toStartOf="@+id/tvLocation"
        app:layout_constraintTop_toBottomOf="@+id/tvLocation" />

    <TextView
        android:id="@+id/tvFirstSeen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First seen in:"
        android:layout_marginTop="5dp"
        android:textColor="@color/light_grey"
        app:layout_constraintStart_toStartOf="@+id/tvLocationInput"
        app:layout_constraintTop_toBottomOf="@+id/tvLocationInput" />

    <TextView
        android:id="@+id/tvFirstSeenInput"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Total Rickall"
        android:textColor="@color/white"
        android:textSize="16sp"
        android:paddingBottom="10dp"
        app:layout_constraintStart_toStartOf="@+id/tvFirstSeen"
        app:layout_constraintTop_toBottomOf="@+id/tvFirstSeen" />


    <ImageView
        android:id="@+id/characterPortrait"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="@+id/tvFirstSeenInput"
        app:layout_constraintDimensionRatio="1:1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_launcher_background"
        android:contentDescription="@string/character_portrait_description" />

</androidx.constraintlayout.widget.ConstraintLayout>


The result is this

61547bb704d4d735976270.png

. What can be done so that the ImageView does not change the shape of the list element, but is cut off at the edges?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton, 2021-10-06
@TequilaOne

It is possible to put your list item in a card view . CardView has an option that rounds the corners. Then the ImageView will have left corners cut off

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question