How to Draw Different Types of Circles in Android?
Last Updated :
23 Jan, 2022
In Android, we can manually create shapes as required. A shape can be designed in XML by creating an Android Resource File. The type of file allows various attributes like dimensions, color, strokes (border), solid (background), etc. for creating a desired shape and design. Basically, in this article, we have explained 3 types of circles:
- Circle 1: A simple circle with only a border
- Circle 2: A simple circle with only solid color
- Circle 3: A circle with a border and a solid color.
In this article, we will show you how you could create different types of circles. Now to start with, follow the below steps once the IDE is ready.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Creating Android Resource Files
In this article, we shall be implementing 3 types of Circles. So we created three such resource files for circle 1, circle 2, and circle 3. To create an Android Resource File, right-click on the res folder, go to New and click on Android Resource File as shown below.
Name the file according to the circle. For the first file, we named it circle_1.
Code for circle_1.xml:
Circle 1 is a simple circle that has nobody color and only an outline. For the same, the code is given below.
XML
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="5sp"
android:color="#0f9d58"/>
</shape>
Code for circle_2.xml:
In Circle 2, we have attributed it with only body color. Refer to the code below.
XML
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#0f9d58"/>
</shape>
Code for circle_3.xml:
In this circle, we have combined the above two attributes, i.e., boundary color and body color. The code for the same is given below.Â
XML
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#0f9d58"/>
<stroke
android:width="5sp"
android:color="@color/black"/>
</shape>
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. We shall be displaying all three circles for which we need to add three ImageViews.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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">
<ImageView
android:id="@+id/circle_1"
android:layout_width="100sp"
android:layout_height="100sp"
android:src="@drawable/circle_1"
android:layout_centerHorizontal="true"
android:layout_marginTop="30sp"/>
<ImageView
android:id="@+id/circle_2"
android:layout_below="@id/circle_1"
android:layout_width="100sp"
android:layout_height="100sp"
android:src="@drawable/circle_2"
android:layout_centerHorizontal="true"
android:layout_marginTop="30sp"/>
<ImageView
android:id="@+id/circle_3"
android:layout_below="@id/circle_2"
android:layout_width="100sp"
android:layout_height="100sp"
android:src="@drawable/circle_3"
android:layout_centerHorizontal="true"
android:layout_marginTop="30sp"/>
</RelativeLayout>
No changes in MainActivity.kt. As we are displaying the circles directly into the ImageViews, we shall leave the main file untouched.
Kotlin
package org.geeksforgeeks.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
Output:
You can see the three types of circles that we created previously. Circle 1 has only boundary color, Circle 2 has only body color, and Circle 3 has both boundary and body color.
Similar Reads
How to Create CircularDialog in Android? CircularDialog is another best way of representing information or data in an Android app. You can get to see these Circular Dialogs in most of the apps which are used to display the message of completion of the process in an attractive form. In this article, we are going to see how to implement Circ
3 min read
How to Create Shine Effect in Android? Shine Effect is used to give an ImageView, Button, or a View a better animation look. It is very easy to implement. A sample GIF is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language.  Step by Step Imp
3 min read
How to Create Circular Determinate ProgressBar in Android? In this article, we are going to demonstrate how to create a circular progress bar in Android Studio that displays the current progress value and has a gray background color initially. Here progress is shown in the center of the Bar. A sample GIF is given below to get an idea about what we are going
5 min read
Create Different Types of Circle in Canvas in Android using Jetpack Compose In Android, Canvas is used for creating different types of visual elements, which is most commonly used for creating different types of shapes. In our example, we have created a circle of different types, i.e., the circle with a solid boundary with no fill, the circle with a dotted boundary with no
3 min read
How to Create Custom Shapes of Data Points in GraphView in Android? If you are looking for a view to represent some statistical data or looking for a UI for displaying a graph in your app then in this article we will take a look at creating a line graph view and showing custom shape to the data points in our Android App. Â What we are going to build in this article?
3 min read