In this tutorial, we are going to see how to create a circular image view and a circular image view with border-color in Android. Most of the time the android developers use a third-party library for creating a circular profile image, but this tutorial will explain how to create a circular image view without the help of the third-party library and plugins as well.
Most of the time the usage of third-party libraries degrade the speed and affect the data in an android application.
One of the most common tasks in android is to create a Circular ImageView. The default ImageView in android is rectangle so there are situations where we will have to create a circular ImageView.
There are different ways to achieve this but before that let us see the result of what we have to achieve. The image below represents the circular image view in the android application.
Steps:
1. Adding Support Library
You need to add the following support libraries to Gradle (Module.app) for CardView to work on your project.
Picasso and glide libraries are used for loading the image from the browser or server. Here, we use these libraries following.
2. Creating Circular ImageView without border-color
We have to create an empty activity and name it as activity_main.xml and MainActivity.java for our future use. In this stage, XML acts as a major role in creating a circular ImageView. You can define the XML code as follows…
activity_main.xml
You have to put your ImageView inside the CardView because CardView only has a feature of accessing the corner radius attribute. This corner radius attribute helps us to create a rounded ImageView. If you forget to do the below step, your image won’t look circular. It will either look like a rectangle or rectangle with a corner radius.
IMPORTANT NOTE: CardView app:cardCornerRadius
will double half of your CardView width and height. app:cardCornerRadius
always depends on the height and width attribute of the CardView.
3. Creating Circular ImageView with border-color
- Create drawable.xml for circular shape with border-color
- Rewrite activity_main.xml for border-color
Create drawable.xml for circular shape with border-color
You have to create the cicular_bordershape.xml under the drawable folder. It will set the border color for the ImageView. You can define the XML code as follows…
circular_bordershape.xml
IMPORTANT NOTE: <stroke>
is used to set the border width and as well as the border color.
Rewrite activity_main.xml for border-color
This is how you rewrite the activity_main.xml code for border-color. You can define the XML code as follows…
activity_main.xml
The CardView should be placed inside the RelativeLayout. We use this RelativeLayout to apply the background as drawable.xml for border-color, it gives your ImageView with border-color.
IMPORTANT NOTE:
RelativeLayout height and width calculation formula:-
(dp- Density independent pixels)
4. Load the image using Glide and Picasso tool
As mentioned above, Glide and Picasso libraries are used for loading the images from the server or browser. That’s why we use these libraries for image loading. Custom view also loads the image from the server.
MainActivity.java
This brings us to the end of the blog post. I hope you have learned about creating circular imageview with a border for your android application. Now, you can run this on your android device and see the output for yourself.