Open In App

How to Create CircularDialog in Android?

Last Updated : 31 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 Circular Dialogs in the Android app. 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 Java language. 

CircularDialog in Android Sample GIF

Application of Circular Dialogs

  • Use to represent the message in an attractive form.
  • Circular Dialogs is a unique way of representing messages in the alert dialog box.
  • Create a good quality User Experience.

Attributes of Circular Dialog

Attributes

Description

CDConstants.SUCCESS To display Success message on a Circular Dialog.
 .setAnimation Use to set Set Animation to Circular Dialog.
.setTextSize Use to set Set text size in Circular Dialog.
.setDuration Use to set Animation time in milliSeconds.
 CDConstants.LARGE Use to set the size of Dialog.

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Add dependency and JitPack Repository

Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.   

implementation ‘com.github.hassanusman:CircularDialogs:1.2’

Add the JitPack repository to your build file. Add it to your root build.gradle at the end of repositories inside the allprojects{ } section.

allprojects {

 repositories {

   …

   maven { url “https://jitpack.io” }

     }

}

After adding this dependency sync your project and now we will move towards its implementation.  

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. 

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <!--After clicking button you will
        get circular dialog-->
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="CLICK" />
      
</RelativeLayout>


Step 4: Working with the MainActivity.java file

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

Java




import android.os.Bundle;
import android.view.View;
import android.widget.Button;
  
import androidx.appcompat.app.AppCompatActivity;
  
import com.example.circulardialog.CDialog;
import com.example.circulardialog.extras.CDConstants;
  
public class MainActivity extends AppCompatActivity {
  
    // Initialize button
    Button button;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Called button by Id
        button = (Button) findViewById(R.id.button);
  
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                  
                // Circular Dialog Box Created
                new CDialog(MainActivity.this).createAlert("Great",
                        CDConstants.SUCCESS,   // Type of dialog
                        CDConstants.LARGE)     // Size of dialog
                        .setAnimation(CDConstants.SLIDE_FROM_BOTTOM_TO_TOP)  // Animation for enter/exit
                        .setDuration(2000)   // in milliseconds
                        .setTextSize(CDConstants.LARGE_TEXT_SIZE)  // CDConstants.LARGE_TEXT_SIZE, 
                                                                   // CDConstants.NORMAL_TEXT_SIZE
                        .show();
            }
        });
    }
}


Now click on the run option it will take some time to build Gradle. After that, you will get output on your device as given below.

Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads