Alert dialog box

How to Create Alert Dialog Box in Android

Alert Dialog Box in Android:

The Alert dialog box will display a warning message and then proceed to the next step according to your answer. The Android alert dialog is composed of three fields: title, message area, and Action buttons. The Alert dialog code has three methods: The setTitle() method displays the title of the warning dialog. setmessage() for displaying a message in the alert box and section() is used to set the icon in an alert box.

The Android AlertDialog class can be used to display a dialog message with buttons for OK and Cancel. It can be used to interrupt the user and ask if he or she wants to continue or stop.

Alert Dialog has three methods:

MethodDescription
setTitle()It is used to set the title in the Alert dialog
setMessage()It is used to set messages in the Alert dialog
setIcon()it is used to set icon in the Alert dialog

File: MainActivity.kt

The will display a warning message and then proceed to the next step according to your answer. The Android is composed of three fields: title, message area, and Action buttons. The code has three methods: The setTitle() method displays the title of the warning dialog. setmessage() for displaying a message in the alert box and section() is used to set the icon in an alert box.

–> In MainActivity.kt file implement a override fun onBackPressed() {} function and inside it create a alert dialog and if we press the exit button alert dialog will pop -up.

package com.sagar.alertdialogbox


import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity


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


    override fun onBackPressed() {


        val builder= AlertDialog.Builder(this)

        // Set  message
        builder.setMessage("Alert Box Test ?")

        // Set Title
        builder.setTitle("Alert..")

        //set icon
        builder.setIcon(R.drawable.ic_baseline_warning_24)


        builder.setCancelable(false)

        builder
            .setPositiveButton(
                "Yes"
            ) { dialog, which ->
                Toast.makeText(this, "Back button presses", Toast.LENGTH_SHORT).show()

                finish()
            }

        builder
            .setNegativeButton(
                "No"
            ) { dialog, which ->
                Toast.makeText(this, "No Action taken", Toast.LENGTH_SHORT).show()

                dialog.cancel()
            }


        val alertDialog: AlertDialog = builder.create()

        alertDialog.show()
    }
}

File: activity_main.xml

–> activity_main.xml file contain textview place inside a ConstraintLayout.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/Alert_box_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="292dp"
            android:text="Press Back Button for Alert Dialog Box"
            android:textSize="20dp"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • We hope that this guide will assist you in quickly creating an Alert dialog Android app. If you have any problems, please post them in the comments section and we will gladly assist you.

This Post Has One Comment

Leave a Reply