TheJadav

CustomShapeView in android – Easiest way ever

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

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>
  
  

Share this content:

Share:

More Posts

How does data persistence work in Android?

Data persistence in Android is the ability to save data to the device so that it can be accessed later, even if the app is closed or the device is restarted. There are several ways to achieve data persistence in Android

Fragments and Activities: Two Pillars of Android Development

Fragments and Activities are both important components of Android development. Fragments are a good choice for creating reusable and dynamic UIs, while Activities are a good choice for managing the lifecycle of an app’s UI and implementing core features.

Table of Contents

Send Us A Message