How to create alert dialog in Android Studio using Java?

// Code for displaying alert dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(“Message to be displayed on alert box“)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Your code to handle Yes button click
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Your code to handle No button click
}
});
// Create the AlertDialog
builder.create();
// Show the Dialog
builder.show();
import android.app.AlertDialog;
import android.content.DialogInterface;
  • The alert dialog shows the message “Message to be displayed on alert box”.
  • It has a positive button with text as “Yes”.
  • It has a negative button with text as “No”.

--

--

--

Software Engineer - (Android, iOS and Unity) | M.Sc. Computer Science from Technische Universitaet Berlin | Mobile Advertising, attribution, monetisation

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

DataStore and synchronous work

Spring unit testing

‼️ *Task1: Flutter App Development*‼️

More productivity with Kotlin

Migrate from Groovy to Kotlin DSL

Explore Hilt: A Dependency Injection Library for Android

Create a Huawei Health Integrated Dashboard in Xamarin Forms

AOSP.dev — 2: Building your first Android image (on Ubuntu) _part-1

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akarsh SEGGEMU

Akarsh SEGGEMU

Software Engineer - (Android, iOS and Unity) | M.Sc. Computer Science from Technische Universitaet Berlin | Mobile Advertising, attribution, monetisation

More from Medium

Dive in A Quick Dip — Java (Based on Head First Java Second Edition)

First Java Program

Let’s talk about head first Java

Chapter 2 : A Trip to Objectville