AlertDialog Android Kotlin

AlertDialog Android Kotlin | AlertDialog in Kotlin with Example

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. Lets code an example of AlertDialog Android with Kotlin.

AlertDialog Android Kotlin Example:

Alert Dialog prompts the user to take an 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 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 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:

 

5- Now, come to your MainActivity. kt file and define a value for our 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.

 

8- We set the Title and Message of our Alert Dialog.

 

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.

 

11- Set up a Negative Button with a ClickListener that will display a Toast message.

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.

 

13- Finally create the Dialog and Display it.

 

14- So, The complete final MainActivity.kt file will look like this:

 

So this is it for this tutorial. If you have any queries regarding AlertDialog Android Kotlin then do mention them in comments section below.

OUTPUT

AlertDialog Android Kotlin
AlertDialog Android Kotlin

AlertDialog Android Kotlin

AlertDialog Android Kotlin

 

 

6 thoughts on “AlertDialog Android Kotlin | AlertDialog in Kotlin with Example”

  1. Pingback: How To Check Internet Connection in Android Programmatically

  2. Pingback: Android Custom Toolbar In Kotlin | Toolbar Kotlin Example - My Android

  3. Pingback: Splash Screen Android Kotlin | Working Example Android - My Android

  4. Pingback: Check Internet Connection Programmatically Android Kotlin - My Android

Leave a Comment

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