August 13, 2021

CustomShapeView in android – Easiest way ever

By lj007

CustomShapeView is requirnment for almost every application. I have searched most of time how to make custom shape imageview. Recently I found most easy way of doing it, by using below code

//in xml you need to add attributes to imageview

        android:background="@drawable/bg_circle"
        android:outlineProvider="background"

//Set below line in code file

        imageView.clipToOutline = true

Is it that much easy? Yes, it is.

Note: It is only working for shape drawable (Not vector) and for android >= 21.

You can find full source code here
Here is top rounded corner shape drawable

  
  <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#f6eef1" />

    <stroke
        android:width="2dp"
        android:color="#000000" />

    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />

    <corners android:topLeftRadius="20dp" android:topRightRadius="20dp" />

</shape>