Dialog

Parameters

Remarks

The dialog in the first example(Dialog) does not need to call show() when it is created as it is handled in the constructor

Alert Dialogs must be constructed through a new instance of the AlertDialog.Builder() class. Following the Builder Pattern, all members of the AlertDialog.Builder can be method chained to 'build up' the dialog instance.

The Alert Dialog builder can directly show() the dialog -- you do not need to call create() then show() on the AlertDialog instance

A Basic Alert Dialog

AlertDialog.Builder builder = new AlertDialog.Builder(context);
//Set Title
builder.setTitle("Reset...")
//Set Message
.setMessage("Are you sure?")
//Set the icon of the dialog
.setIcon(drawable)
//Set the positive button, in this case, OK, which will dismiss the dialog and do everything in the onClick method
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// Reset
}
});
AlertDialog dialog = builder.create();
//Now, any time you can call on:
dialog.show();
//So you can show the dialog.

Now this code will achieve this:
(Image source: WikiHow)

Adding Material Design AlertDialog to your app using Appcompat

AlertDialog is a subclass of Dialog that can display one, two or three buttons. If you only want to display a String in this dialog box, use the setMessage() method.

The AlertDialog from android.app package displays differently on different Android OS Versions.

The Android V7 Appcompat library provides an AlertDialog implementation which will display with Material Design on all supported Android OS versions, as shown below:

First you need to add the V7 Appcompat library to your project. you can do this in the app level build.gradle file:

ListView in AlertDialog

We can always use ListView or RecyclerView for selection from list of items, but if we have small amount of choices and among those choices we want user to select one, we can use AlertDialog.Builder setAdapter.