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:
As you know Android AlertDialog is the subclass of Dialog class. So you can use Dialog class methods with AlertDialog.
Method | Description |
---|---|
public AlertDialog.Builder setTitle(CharSequence) | This method is used to set the title of AlertDialog. |
public AlertDialog.Builder setMessage(CharSequence) | This method is used to set the message for AlertDialog. |
public AlertDialog.Builder setIcon(int) | This method is used to set the icon over AlertDialog. |
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.java
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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | package com.example.alertdialogexample; import androidx.appcompat.app.AppCompatActivity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { //Create button sample Button btnMessage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Get the button view btnMessage = findViewById(R.id.buttonAlert); //set the button click btnMessage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //fire the alert dialog :) openAlert(view); } }); } //Open Alert Dialog when the button clicked. private void openAlert(View view) { AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this); alertDialogBuilder.setTitle(this.getTitle()+ " Decision"); alertDialogBuilder.setMessage("What do you want to?"); // set Positive button alertDialogBuilder.setPositiveButton("Yees",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { // Create a new intent and open. Intent positveActivity = new Intent(getApplicationContext(), PositiveAnswer.class); startActivity(positveActivity); } }); // set Negative button alertDialogBuilder.setNegativeButton("Nooo",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { // when the dialog cancel, show the Toast message dialog.cancel(); Toast.makeText(getApplicationContext(), "You canceled the decision", Toast.LENGTH_LONG).show(); } }); // set neutral button alertDialogBuilder.setNeutralButton("Close APP",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) { // Close the app MainActivity.this.finish(); } }); AlertDialog alertDialog = alertDialogBuilder.create(); // Show the AlertDialog alertDialog.show(); } } |
[…] You may also like: Android Alert Dialog Example […]
[…] You may also like: Android Alert Dialog Example […]