In this tutorial, we show you how to display an alert box in Android. See flowing Steps :
AlertDialog is a structure extended from Dialog that allows the user to see a popup image on the screen. We can show it as an information message to the user, or we can use the AlertDialog structure as an error message if we want.
Demo:
Android Alert Dialog Example
Step 1: Open the Android Studio IDE and Create a New Empty Activity. Name it similarly.
Step 2: Simple layout file, display a button on screen.
File : res/layout/activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?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"> <Button android:id="@+id/buttonAlert" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show Alert Box" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> |
Step 3: Step 3: Create an Intent to be shown in case of positive response.
Create a New Activity into App folder by right click. New → Activity → Empty Activity. Name as PositiveAnswer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?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=".PositiveAnswer"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Positive Response" android:textSize="34sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> |
Step 4: Let’s write the code to create and show the AlertDialog.
File: 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | package com.example.codeexample import android.app.AlertDialog import android.content.Intent import android.os.Bundle import android.view.View import android.widget.Button import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) //Get the button view var btnMessage:Button = findViewById(R.id.buttonAlert) //set the button click //set the button click btnMessage.setOnClickListener(View.OnClickListener { view -> //fire the alert dialog :) openAlert(view) }) } //Open Alert Dialog when the button clicked. private fun openAlert(view: View) { val alertDialogBuilder = AlertDialog.Builder(this@MainActivity) alertDialogBuilder.setTitle(this.title.toString() + " Decision") alertDialogBuilder.setMessage("What do you want to?") // set Positive button alertDialogBuilder.setPositiveButton( "Yees" ) { dialog, id -> // Create a new intent and open. val positveActivity = Intent( applicationContext, PositiveAnswer::class.java ) startActivity(positveActivity) } // set Negative button alertDialogBuilder.setNegativeButton( "Nooo" ) { dialog, id -> // when the dialog cancel, show the Toast message dialog.cancel() Toast.makeText( applicationContext, "You canceled the decision", Toast.LENGTH_LONG ).show() } // set neutral button alertDialogBuilder.setNeutralButton( "Close APP" ) { dialog, id -> // Close the app finish() } val alertDialog = alertDialogBuilder.create() // Show the AlertDialog alertDialog.show() } } |