Search code examples
androidandroid-layoutrounded-corners

Imageview with rounded corner using kotlin


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


Solution

  • 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"