Dialogs

A dialog is a small window that prompts the user to make a decision or enter additional information. A dialog does not fill the screen and is normally used for modal events that require users to take an action before they can proceed.

The Dialog class is the base class for dialogs. Activities provide a facility to manage the creation, saving and restoring of dialogs via methods onCreateDialog(), onPrepareDialog, showDialog, and dismissDialog()methods. But at nowdays DialogFragment is used.

There are built-in dialogs:

  • AlertDialog - can show a title, up to three buttons, a list of selectable items, or a custom layout. You can create dialog with AlertDialog.Builder class.
  • DatePickerDialog - allows to select date.
  • TimePickerDialog - allows to select time.

DialogFragment is a special fragment, that can be shown as a dialog window or embedded into UI as normal fragment. For example, on a small screen you can show it as embedded and on a large screen as dialog.

// show as dialog
DialogFragment newFragment = MyDialogFragment.newInstance();
newFragment.show(getFragmentManager(), "dialog");

You can create a dialog object in DialogFragment, for example using AlertDialog.Builder. In this case, the fragment can only be shown as a dialog and not as an embedded fragment.

AlertDialog example
class FireMissilesDialogFragment : DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle): Dialog {
        return activity?.let {
            // Use the Builder class for convenient dialog construction
            val builder = AlertDialog.Builder(it)
            builder.setMessage(R.string.dialog_fire_missiles)
                    .setPositiveButton(R.string.fire,
                            DialogInterface.OnClickListener { dialog, id ->
                                // FIRE ZE MISSILES!
                            })
                    .setNegativeButton(R.string.cancel,
                            DialogInterface.OnClickListener { dialog, id ->
                                // User cancelled the dialog
                            })
            // Create the AlertDialog object and return it
            builder.create()
        } ?: throw IllegalStateException("Activity cannot be null")
    }
}
public class FireMissilesDialogFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.dialog_fire_missiles)
               .setPositiveButton(R.string.fire, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // FIRE ZE MISSILES!
                   }
               })
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();
    }
}
TimePickerDialog example
DatePickerDialog example
AlertDialog single-choice list example
AlertDialog multiple-choice list example