androidxmlandroid-layoutandroid-imageview

Imageview not showing rounded corners


enter image description here

I am trying to make my layout like this but i am not able to make my imageview like this with rounded corners. It shows perfectly in Android studio's layout editors but in real device i am getting flat corners. I tired via XML and also with code but none working for me. Please help me to make my layout like this sample image.

My layout file

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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_height="match_parent"
    android:layout_width="match_parent"
    android:clickable="true"
    android:background="?android:attr/colorBackground"
    android:focusable="true">

    <androidx.constraintlayout.widget.ConstraintLayout

        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:paddingBottom="10dp"
        android:paddingTop="10dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:paddingStart="5dp"
        android:paddingEnd="5dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true">

        <ProgressBar

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/User_CoverPhoto"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            />

        <ImageView

            android:layout_width="150dp"
            android:layout_height="match_parent"
            android:layout_marginEnd="8dp"
            android:background="@drawable/round_shape_only"
            android:scaleType="centerCrop"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/User_CoverPhoto"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <ProgressBar

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/User_ProfilePictures"
            app:layout_constraintTop_toTopOf="parent"
            />
        <ImageView

            android:layout_width="230dp"
            android:layout_height="match_parent"
            android:background="@drawable/round_shape_only"
            android:scaleType="centerCrop"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/User_ProfilePictures"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:background="#40000000"
            android:backgroundTint="#CC000000"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:text="Profile Picture"
            android:textColor="@color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@+id/User_ProfilePictures"
            app:layout_constraintHorizontal_bias="0.01"
            app:layout_constraintStart_toStartOf="@+id/User_ProfilePictures" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:background="#40000000"
            android:backgroundTint="#CC000000"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:text="Cover Photo"
            android:textColor="@color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="@+id/User_CoverPhoto" />

    </androidx.constraintlayout.widget.ConstraintLayout>


    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:clipToPadding="false"
        android:paddingTop="13dp"

        android:id="@+id/Grid_Recycler">

    </androidx.recyclerview.widget.RecyclerView>



</RelativeLayout>

XML for round shape

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#CCCCCC"/>
    <padding android:left="1dp"
        android:top="1dp"
        android:right="1dp"
        android:bottom="1dp"/>


    <corners android:radius="15dp"/>
</shape>

Solution

  • Just use the ShapeableImageView in the Material Components library.
    Something like:

      <com.google.android.material.imageview.ShapeableImageView
          ...
          app:shapeAppearanceOverlay="@style/roundedCornersImageView"
          app:srcCompat="@drawable/ic_image" />
    

    with:

      <style name="roundedCornersImageView" parent="">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSize">xxdp</item>
      </style>
    

    enter image description here

    *Note: it requires at least the version 1.2.0-alpha03.