Imageview with rounded corner using kotlin

10,322

Create res/drawable/round_outline.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="10dp" />
</shape>

Set the drawable as your ImageView's background

android:background="@drawable/round_outline"

On ImageView (layout)

android:clipToOutline="true"
Share:
10,322

Related videos on Youtube

VigneshK
Author by

VigneshK

Updated on June 04, 2022

Comments

  • VigneshK
    VigneshK about 2 years

    I have created the layout with Imageview and I have set custom background with corner radius, the parent layout is cornered but the Imageview is not rounded cornered. How can I achieve this?

    Thanks in advance.

    bg.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"
        >
        <solid
            android:color="@color/list_item_bg"/>
    
        <stroke
            android:width="2dp"
            android:color="@color/colorAccent" />
        <corners
            android:topLeftRadius="20dp"
            android:topRightRadius="20dp"/>
    
    </shape>
    

    list_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/list_item_bg"
        >
    
        <ImageView
            android:id="@+id/food_image"
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_marginRight="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="2dp"
            android:scaleType="centerCrop"
            android:clipToOutline="true"
            android:background="@drawable/image_bg"
            android:src="@drawable/sample_image"/>
    </LinearLayout>
    

    enter image description here

    • Molly
      Molly over 5 years
      share your layout code.
    • Kevin Kurien
      Kevin Kurien over 5 years
      this should help link
  • VigneshK
    VigneshK over 5 years
    I have tried the above code but I got the same issue
  • Anees
    Anees over 5 years
    please provider the xml layout file
  • VigneshK
    VigneshK over 5 years
    I have added the code in my question.
  • Clean Coder
    Clean Coder almost 3 years
    clipToOutline is not found for imageview tag. So i followed this library: implementation 'com.makeramen:roundedimageview:2.3.0' . It works both with Java and Kotlin. Just replace your imageview with : <com.makeramen.roundedimageview.RoundedImageView android:layout_width="113dp" android:layout_height="113dp" ...etc.