Alert Dialog Android is a Dialog box that prompts a user to take an action. It does not fill the screen but appears in the center as a message before a user can proceed. In this example, we will learn how to make Android Alert Dialog Example.
Normally Alert dialog has two buttons POSITIVE and NEGATIVE. We can also use a neutral button in Alert Dialog. In Alert Dialog in the picture below you can see an Alert Dialog with three buttons. POSITIVE, NEGATIVE, and NEUTRAL buttons.

Alert Dialog is used when a user wants to decide an action between two options. Like we see in almost all android apps. For example, in an android phone if a user wants to uninstall an application the android system prompts a message. It asks the user if he/she “wants to remove the selected application or not”. So in easy words such a message box that prompts a user to take an action is called an ALERT DIALOG BOX.
You can use some other methods to customize the appearance of your Alert Dialog.
1- setIcon(Drawable icon)
2- setIcon(int resId)
3- setTitle(CharSequence title)
4- setMessage(CharSequence message)
5- setView(View view)
Let’s learn How to Design a simple Alert Dialog with Android Alertdialog example:
1- So create a new Android Studio Project and name it “Alert Dialog”. Or you can name it anything you want. After the project builds go to the “activity_main.xml” file.
2- But, here in the “activity_main.xml” file first change the Constraint Layout into Relative Layout.
3- After Changing the Layout into Relative Layout add the “activity_main.xml” code into your “activity_main.xml” file.
4- After that, come to your main file “MainActivity.java” . Add “MainActivity.java” code into your “MainActivity.java” file.
5- But, you can change the message in Toast and set it according to your choice.
6- After Using the following blocks of code you will see an Alert Dialog as output with two buttons.
Other names are also used for Android Dialog such as Alert Dialog, Android Alert, and Android Studio dialog.
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?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"> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="ALERT DIALOG" android:textSize="25sp" android:textStyle="bold"/> </RelativeLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
package com.example.alertdialog; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("WARNING"); builder.setMessage("Are You Sure You Want TO Exit ...."); builder.setIcon(R.drawable.ic_warning_black_24dp); builder.setCancelable(true); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.cancel(); Toast.makeText(MainActivity.this, "Closed", Toast.LENGTH_SHORT).show(); } }); builder.create().show(); } }); } |
So, here we complete our Android dialog example. I hope this Android AlertDialog Example will help you understand more about the Android Alert Dialog example. Then, after all this code finally, the output will be like this.
OUTPUT

Android Studio Dialog Example
People Also Read
- Horizontal Scroll View With Android
- Programmatically Check And Verify Internet Connection With KOTLIN
- Android With Custom Toolbar And Kotlin