Alert Dialog Android and Example in Kotlin

A Dialog is a small User Interface window, which popup on the app for the user to make a decision or fill any additional information. Where Alert Dialog Android can be a subcategory of Dialog. Like if you want to send the Email? That time its give to user make the decision to take action or not (Yes or no option buttons). One more example mostly android apps are using a back press button and popup close app confirmation alert dialog.

Alert Dialog Android and Example in Kotlin

In this tutorial with example, you will learn

  1. About Alert Dialog Android
  2. Create and show Alert Dialog box
  3. Add Listeners on buttons

Building an Alert Dialog and Important fields

The AlertDialog class provides you to build a variety of dialog designs and is many times the only dialog class you will need. There are 3 fields of an alert dialog:

  1. Title – This is optional and should be used only when the content area is occupied by a detailed message, a list, or custom layout. If you need to state a simple message or question, you don’t need a title.
  2. Content area – This can display a message, a list, or any custom layout.
  3. Action buttons – A button with the option to do some action like Yes, No, Cancel, etc. Buttons should be no more than three action buttons in a dialog.

Let’s Build an Alert Dialog Android example in kotlin:

Step 1. Create a new project “Build Your First Android App in Kotlin
Step 2. Add following code in “activity_main.xml” layout file

Where button has a method to perform a Click action.

Step 3. Open the “MainActivity.kt” and add following code

Hereby clicking the button – dialog is creating and showing

Step 4. Now Run the application, in an emulator or On you android device

Output screenshot Android Alert Dialog :

Alert Dialog Android and Example in Kotlin output

Download source code Alert Dialog Android in kotlin

Note: This example (Project) is developed in Android Studio 3.1.3. Tested on Android 9 ( Android-P), compile SDK version API 27: Android 8.0 (Oreo)



Coding in Kotlin

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.