Java: 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”.

--

--

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, Unity) | M.Sc. Computer Science — Technische Universitaet Berlin | Mobile Advertising, Attribution, Monetisation, UA & Analytic