AlertDialog in Android is a Dialog box that (prompts) asks a user to take an action. It prompts before proceeding ahead. By default, it appears in the center of the screen. Alert Dialog displays a Title, a Message, and Buttons (up to three). They are Positive (Yes), Negative (No), and Neutral (Cancel) Button. Let’s code an example of AlertDialog Android with Kotlin.
AlertDialog Android Kotlin Example
Alert Dialog prompts the user to take any action before proceeding to the next step. Because it’s a very common feature so you can see it in almost every Android application.
Alert Dialog has the following components:
- Title.
- Message.
- Buttons (Positive, Negative, and Neutral).
Methods Used in AlertDialog are:
- setTitle()
- setMessage()
- setPositiveButton()
- setNegativeButton()
- setNeutralButton()
1- So, create a new Android Studio Project and name it AlertDialogKotlin. Also, you can name it anything you want. Set Kotlin as your default language. After the project builds go to the activity_main.xml file.
2- In your activity_main.xml file, first change the default Constraint Layout to Relative Layout.
3- In your Relative Layout add a Button with wrap_content width and height.
4- After adding some properties to Button, the final activity_main.xml will look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?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/alert_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="25sp" android:textColor="#000000" android:fontFamily="sans-serif-medium" android:text="Show Alert Dialog BOX" /> </RelativeLayout> |
5- Now, come to your MainActivity.kt file and define a value for our button.
1 |
val alertButton = findViewById<Button>(R.id.alert_button) |
6- Now, we add an OnClickListener on our Button.
7- We use an Instance of AlertDialog.Builder class and hold it in val with the name “builder”. Pass “this” as a context of your MainActivity.
1 2 3 4 |
// Performs action on Button Click alertButton.setOnClickListener{ // Initialization of Instance of AlertDialog.Builder val builder = AlertDialog.Builder(this@MainActivity) |
8- We set the Title and Message of our Alert Dialog.
1 2 3 4 5 |
// Sets TITLE for Alert Dialog Box builder.setTitle("ALERT DIALOG BOX") // Sets the message you want to display builder.setMessage("Do You Want to Uninstall this application?") |
9- After setting up the Title and Message we will then set up the Buttons.
10- We set up a Positive Button with a ClickListener that will display a Toast message.
1 2 3 4 5 6 7 |
// Creates a positive Button with a Click Listener builder.setPositiveButton("YES"){dialog, which -> // Displays a Toast Message on Positive Button click Toast.makeText(applicationContext,"The Appliciation will be uninstalled now", Toast.LENGTH_SHORT).show() } |
11- Set up a Negative Button with a ClickListener that will display a Toast message.
1 2 3 4 5 6 |
// Creates a Negative Button with a Click Listener builder.setNegativeButton("No"){dialog,which -> Toast.makeText(applicationContext, "You Stopped the Uninstall process",Toast.LENGTH_SHORT).show() } |
12- Now we set up a Neutral Button with a ClickListener because the neutral button cancels the dialog. It is used when there is a choice to skip.
1 2 3 4 5 |
/ Creates a Negative Button with a Click Listener builder.setNegativeButton("No"){dialog,which -> Toast.makeText(applicationContext,"You Stopped the Uninstall process", Toast.LENGTH_SHORT).show() } |
13- Finally create the Dialog and Display it.
1 2 3 |
// Creates an Alert Dialog and Displays it on the screen val dialog: AlertDialog = builder.create() dialog.show() |
14- So, The complete final MainActivity.kt file will look like this:
Kotlin Alertdialog Example
MainActivity.kt
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 |
package com.example.alertdialogkotlin import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.Button import android.widget.Toast import androidx.appcompat.app.AlertDialog class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val alertButton = findViewById<Button>(R.id.alert_button) // Performs action on Button Click alertButton.setOnClickListener{ // Initialization of Instance of AlertDialog.Builder val builder = AlertDialog.Builder(this@MainActivity) // Sets TITLE for Alert Dialog Box builder.setTitle("ALERT DIALOG BOX") // Sets the message you want to display builder.setMessage("Do You Want to Uninstall this application?") // Creates a positive Button with a Click Listener builder.setPositiveButton("YES"){dialog, which -> // Displays a Toast Message on Positive Button click Toast.makeText(applicationContext,"The Appliciation will be uninstalled now", Toast.LENGTH_SHORT).show() } // Creates a Negative Button with a Click Listener builder.setNegativeButton("No"){dialog,which -> Toast.makeText(applicationContext,"You Stopped the Uninstall process", Toast.LENGTH_SHORT).show() } // Creates a Neutral Button with a Click Listener builder.setNeutralButton("Cancel"){_,_ -> Toast.makeText(applicationContext,"You Cancelled.",Toast.LENGTH_SHORT).show() } // Creates an Alert Dialog and Displays it on the screen val dialog: AlertDialog = builder.create() dialog.show() } } } |
So this is it for this Alertdialog kotlin example. If you have any queries regarding AlertDialog Android Kotlin then do mention them in the comments section below.
OUTPUT

Read More
- MediaPlayer Example In Kotlin
- How To Make Login Screen In Kotlin
- Kotlin Speech To Text Example
- ListView In Android Using Kotlin
- Toast Android Studio Kotlin
This blog was… how do I say it? Relevant!! Finally I have found something that helped me. Kudos!
thanks @Rodger 🙂