Javatpoint Logo
Javatpoint Logo

Kotlin Android AlertDialog

Android AlertDialog is a subclass of Dialog class. It is used to prompt a small dialog window to make a decision before a user can proceed in the same activity without changing the screen. A dialog shows the title, message, up to three buttons or a custom layout.

The instance of AlertDialog.Builder class is used to make an alert dialog. Through alert dialog, we create positive (yes), negative (no) and neutral (cancel) decision.

Kotlin Android AlertDialog Example

In this example, we prompt an alert dialog window by clicking a button. This dialog window sets three decision actions as positive, negative and neutral.

activity_main.xml

Add the following code in the activity_main.xml file. In this layout file, we add a button to prompt an alert dialog.

strings.xml

In the strings.xml file add the following code.

MainActivity.kt

Add the following code in the MainActivity.kt class. The instance of AlertDialog.Builder class call the setTitle(), setMessage(), setIcon() methods to set the dialog title, message, icon respectively.

To set the action on alert dialog call the setPositiveButton(), setNeutralButton() and setNegativeButton() methods for positive, neutral and negative action respectively. The show() method of AlertDialog.Builder is used to display the alert dialog.

Output:

Kotlin Android AlertDialog Kotlin Android AlertDialog
Kotlin Android AlertDialog
Next Topic#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA