5

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>
Dinesh
  • 1,410
  • 2
  • 16
  • 29
Ritu
  • 518
  • 2
  • 12
  • 35

6 Answers6

29

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.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
5

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

Almaz_KhR
  • 451
  • 1
  • 5
  • 12
3

you can put your ImageView to a CardView and use the app:cardCornerRadiusfor set image corner

2

You should call setClipToOutline to true when using background with radius it only calls programmatically can not set by XML

imageView.setClipToOutline(true);
Công Hải
  • 4,961
  • 3
  • 14
  • 20
1

For Rounded Corner you can use following Custom Layout:

Java : RoundedCornerImageLayout.java

public class RoundedCornerImageLayout extends FrameLayout {
    private final static float CORNER_RADIUS = 10.0f;
    private float cornerRadius;

    public RoundedCornerImageLayout(Context context) {
        super(context);
        init(context, null, 0);
    }

    public RoundedCornerImageLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public RoundedCornerImageLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
    }

    private void init(Context context, AttributeSet attrs, int defStyle) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        cornerRadius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, CORNER_RADIUS, metrics);
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }


    @Override
    protected void dispatchDraw(Canvas canvas) {
        int count = canvas.save();

        final Path path = new Path();
        path.addRoundRect(new RectF(0, 0, canvas.getWidth(), canvas.getHeight()), cornerRadius, cornerRadius, Path.Direction.CW);
        canvas.clipPath(path, Region.Op.INTERSECT);

        canvas.clipPath(path);
        super.dispatchDraw(canvas);
        canvas.restoreToCount(count);
    }
}

You can change the private final static float CORNER_RADIUS = 10.0f; according to your requirement

in XMl file

<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="match_parent"
    tools:context=".RoundedImageViewActivity">
    <com.example.stackdemo.ui.RoundedCornerImageLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">
        <ImageView
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:src="@drawable/ocean"
            android:scaleType="centerCrop"/>
    </com.example.stackdemo.ui.RoundedCornerImageLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Output for above code is:

enter image description here

I hope this helps you

Android Geek
  • 8,956
  • 2
  • 21
  • 35
0

Use androidx.cardview.widget.CardView in such way:

<androidx.cardview.widget.CardView
                    android:layout_width="148dp"
                    android:layout_height="148dp"
                    card_view:cardCornerRadius="4dp"
                    >

                    <ImageView
                        android:id="@+id/some_id"
                        android:layout_width="148dp"
                        android:layout_height="148dp"
                        android:layout_gravity="center_vertical"
                        android:radius="10dp"
                        android:scaleType="fitXY"
                        tools:background="#EDEDED" />
                </androidx.cardview.widget.CardView>

card_view will be unresolved, so in order to resolve it, add xmlns:card_view="http://schemas.android.com/apk/res-auto" to the root element.

Here's the link to docs: https://developer.android.com/guide/topics/ui/layout/cardview#CardView

Konstantin Kozirev
  • 944
  • 1
  • 10
  • 23