new AlertDialog.Builder(this) .setTitle("Alert Dialog") .setMessage("This is a basic alert dialog.") .setPositiveButton(android.R.string.ok, null) .show();
new AlertDialog.Builder(this) .setTitle("Confirmation") .setMessage("Do you really want to delete this item?") .setPositiveButton(android.R.string.yes, (dialog, which) -> { // Delete item }) .setNegativeButton(android.R.string.no, null) .show();
LayoutInflater inflater = LayoutInflater.from(this); View customView = inflater.inflate(R.layout.custom_dialog, null); new AlertDialog.Builder(this) .setTitle("Custom Dialog") .setView(customView) .setPositiveButton(android.R.string.ok, null) .show();This example creates a custom dialog using a layout file called "custom_dialog". The layout includes a text view, edit text, and checkbox. The dialog has one button labeled "OK" and clicking it dismisses the dialog. Package library: android.app Overall, the android.app.AlertDialog class is a useful tool for displaying dialogs to the user in Android applications.