ImageView with Rounded Corners in Android Android by Rajesh Kumar Sahanee - October 25, 2019October 25, 20190 Post Views: 7,709 Hello Friends, Today I am going to share how to add ImageView with Rounded Corners in Android. Actually recently I was working on an app in which I had to set profile pic of user but with rounded corners. I struggled a lot for this simple thing, but found the solution. So now I am sharing this code to help others as well as myself for future. There is code for both local and online image resources for making it round. So, here is the code. MainActivity.java MainActivity.java Java package com.zatackcoder.roundimage; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.drawable.RoundedBitmapDrawable; import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.widget.ImageView; import java.io.InputStream; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView imageLocal = findViewById(R.id.image_local); ImageView imageOnline = findViewById(R.id.image_online); //for local image resource Bitmap batmapBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.gull); RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), batmapBitmap); circularBitmapDrawable.setCircular(true); imageLocal.setImageDrawable(circularBitmapDrawable); //for online image resource new LoadAndShowImage(imageOnline).execute("https://upload.wikimedia.org/wikipedia/commons/9/9a/Gull_portrait_ca_usa.jpg"); } class LoadAndShowImage extends AsyncTask<String, Void, RoundedBitmapDrawable> { ImageView imageView; public LoadAndShowImage(ImageView imageView) { this.imageView = imageView; } protected RoundedBitmapDrawable doInBackground(String... urls) { String imageURL = urls[0]; RoundedBitmapDrawable roundedBitmapDrawable = null; try { InputStream in = new java.net.URL(imageURL).openStream(); Bitmap bimage = BitmapFactory.decodeStream(in); roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bimage); roundedBitmapDrawable.setCircular(true); } catch (Exception e) { Log.e("LoadAndShowImage", e.getMessage()); } return roundedBitmapDrawable; } protected void onPostExecute(RoundedBitmapDrawable result) { imageView.setImageDrawable(result); } } } 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 package com.zatackcoder.roundimage; import androidx.appcompat.app.AppCompatActivity;import androidx.core.graphics.drawable.RoundedBitmapDrawable;import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory; import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.AsyncTask;import android.os.Bundle;import android.util.Log;import android.widget.ImageView; import java.io.InputStream; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView imageLocal = findViewById(R.id.image_local); ImageView imageOnline = findViewById(R.id.image_online); //for local image resource Bitmap batmapBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.gull); RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), batmapBitmap); circularBitmapDrawable.setCircular(true); imageLocal.setImageDrawable(circularBitmapDrawable); //for online image resource new LoadAndShowImage(imageOnline).execute("https://upload.wikimedia.org/wikipedia/commons/9/9a/Gull_portrait_ca_usa.jpg"); } class LoadAndShowImage extends AsyncTask<String, Void, RoundedBitmapDrawable> { ImageView imageView; public LoadAndShowImage(ImageView imageView) { this.imageView = imageView; } protected RoundedBitmapDrawable doInBackground(String... urls) { String imageURL = urls[0]; RoundedBitmapDrawable roundedBitmapDrawable = null; try { InputStream in = new java.net.URL(imageURL).openStream(); Bitmap bimage = BitmapFactory.decodeStream(in); roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), bimage); roundedBitmapDrawable.setCircular(true); } catch (Exception e) { Log.e("LoadAndShowImage", e.getMessage()); } return roundedBitmapDrawable; } protected void onPostExecute(RoundedBitmapDrawable result) { imageView.setImageDrawable(result); } }} activity_main.xml activity_main.xml XHTML <?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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Local Image"/> <ImageView android:id="@+id/image_local" android:layout_width="200dp" android:layout_height="200dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Online Image"/> <ImageView android:id="@+id/image_online" android:layout_width="200dp" android:layout_height="200dp"/> </LinearLayout> 1234567891011121314151617181920212223242526272829303132 <?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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Local Image"/> <ImageView android:id="@+id/image_local" android:layout_width="200dp" android:layout_height="200dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Online Image"/> <ImageView android:id="@+id/image_online" android:layout_width="200dp" android:layout_height="200dp"/> </LinearLayout> AndroidManifest.xml AndroidManifest.xml XHTML <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zatackcoder.roundimage"> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 1234567891011121314151617181920212223 <?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zatackcoder.roundimage"> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> Screenshot AndroidStudio Project Download ImageView with Rounded Corners in Android 1 file(s) 11.69 MB Download Thanks for Stoping by If you find this helpful then please share