AlertDialog

AlertDialog
A type of dialog in Android that can display a message to the user, along with buttons for user interaction. It is commonly used to prompt the user for confirmation or to display important information.

Characteristics
Customizable: You can set the title, message, buttons, and icon.
Modal: It blocks interaction with the underlying activity until the dialog is dismissed.
Flexible Layout: Supports various layouts, allowing for custom views.
Multiple Button Options: Can include positive, negative, and neutral buttons for user responses.

Examples
1. Basic AlertDialog:
java
new AlertDialog.Builder(context)
.setTitle("Alert")
.setMessage("This is an alert message.")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Handle OK button click
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Handle Cancel button click
}
})
.show();

  1. AlertDialog with Custom View:
    “`java
    LayoutInflater inflater = getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.custom_dialog, null);

new AlertDialog.Builder(context)
.setView(dialogView)
.setTitle(“Custom Dialog”)
.setPositiveButton(“Submit”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Handle Submit button click
}
})
.setNegativeButton(“Cancel”, null)
.show();
“`

Comments