Android Alert Dialog Example

Android Alert Dialog Example | Alert Dialog in Android Studio Example

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.

 

Android Alert Dialog Example
Android Alert Dialog Example

 

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     

                                

 


MainActivity.java

 

 

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 Alert Dialog Example
Android Alert Dialog With Example

 

 

Android Studio Dialog Example

 

     People Also Read

 

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!