Skip to content

Alert Dialog Android and Example in Kotlin

  • by

A Dialog is a small User Interface window, which popup on the app for the user to make a decision or fill in any additional information. Where Alert Dialog Android can be a subcategory of Dialog. Like if you want to send the Email? That time it’s given to the user to 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 the 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 a 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.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="Hello World!"
        app:layout_constraintHorizontal_bias="0.535"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:onClick="showdialog"
        android:text="Show Alert Dialog"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

</android.support.constraint.ConstraintLayout>
Step 3. Open the “MainActivity.kt” and add following code

Hereby clicking the button – dialog is creating and showing

package `in`.eyehunt.alertdialogandroid

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import android.content.DialogInterface
import android.support.v7.app.AlertDialog
import android.view.View


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    fun showdialog(view: View) {

        val alertDialog = AlertDialog.Builder(this)
                //set icon
                .setIcon(android.R.drawable.ic_dialog_alert)
                //set title
                .setTitle("Are you sure to Exit")
                //set message
                .setMessage("If yes then application will close")
                //set positive button
                .setPositiveButton("Yes", DialogInterface.OnClickListener { dialog, i ->
                    //set what would happen when positive button is clicked
                    finish()
                })
                //set negative button
                .setNegativeButton("No", DialogInterface.OnClickListener { dialogInterface, i ->
                    //set what should happen when negative button is clicked
                    Toast.makeText(applicationContext, "Nothing Happened", Toast.LENGTH_LONG).show()
                })
                .show()
    }
}
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

https://github.com/EyeHunts/AlertDialogAndroid

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)

MinSdkVersion=”15″

TargetSdkVersion=”27″

Coding in Kotlin

Leave a Reply

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