Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 651
Ritu Avatar asked Dec 02 '22 09:12

Ritu


2 Answers

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.

like image 71
Gabriele Mariotti Avatar answered Dec 05 '22 09:12

Gabriele Mariotti


You can use Glide

Just write extension function for ImageView:

  1. To make rectangle image with rounded corners

     fun ImageView.loadImageWithCustomCorners(@DrawableRes resId: Int, radius: Int) =
         Glide.with(this)
             .load(resId)
             .transform(RoundedCorners(radius))
             .into(this)
    
  2. To make square image with rounded corners

     fun ImageView.loadImageWithCustomCorners(@DrawableRes resId: Int, radius: Int) =
         Glide.with(this)
             .load(resId)
             .transform(CenterCrop(), RoundedCorners(radius))
             .into(this)
    

Using in code

iv_service_avatar.loadImageWithCustomCorners(R.drawable.ic_default_avatar, 15)

iv_service_avatar it`s ImageView ID

like image 20
Almaz_KhR Avatar answered Dec 05 '22 10:12

Almaz_KhR