Support free tutorials:











vogella training Training Books



Using dialogs in Android programming - Tutorial

Lars Vogel

Version 9.9

27.12.2012

Revision History
Revision 0.1 - 9.9 04.07.2009 - 02.01.2014   created, bug fixing and enhancements

Using dialogs in Android programming

This tutorial describes how to use dialogs in Android applications. It is based on Eclipse 4.2, Java 1.6 and Android 4.4.


Table of Contents

1. Android Dialogs
1.1. Using dialogs in Android
1.2. ProgressDialog
1.3. Custom dialogs
2. Exercise: Displaying an alert dialog
2.1. Adding a dialog to your activity
2.2. Test dialog usage
3. Support free vogella tutorials
3.1. Thank you
3.2. Questions and Discussion
4. Links and Literature
4.1. Source Code
4.2. Android Resources
4.3. vogella Resources

1. Android Dialogs

1.1. Using dialogs in Android

You can open dialogs from your activity via the showDialog(int) method. Dialogs which are created via an activity are bound to this activity. A dialog gets the focus until the user closes it.

The Dialog class is the base class for dialogs. Typically you use one of its subclasses, e.g., AlertDialog, ProgressDialog, DatePickerDialog or TimePickerDialog.

// constant for identifying the dialog
private static final int DIALOG_ALERT = 10;

public void onClick(View view) {
    showDialog(DIALOG_ALERT);
} 

If the dialog is displayed, the Android system calls the onCreateDialog(int) method. In this method you instantiate the correct dialog based on the input parameter. You should always create a dialog from the onCreateDialog(int) method as in this case the Android system manages the dialog for you.

@Override
protected Dialog onCreateDialog(int id) {
  switch (id) {
    case DIALOG_ALERT:
    Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("This will end the activity");
    builder.setCancelable(true);
    builder.setPositiveButton("I agree", new OkOnClickListener());
    builder.setNegativeButton("No, no", new CancelOnClickListener());
    AlertDialog dialog = builder.create();
    dialog.show();
  }
  return super.onCreateDialog(id);
}

private final class CancelOnClickListener implements
  DialogInterface.OnClickListener {
  public void onClick(DialogInterface dialog, int which) {
    Toast.makeText(getApplicationContext(), "Activity will continue",
    Toast.LENGTH_LONG).show();
  }
}

private final class OkOnClickListener implements
  DialogInterface.OnClickListener {
  public void onClick(DialogInterface dialog, int which) {
  AlertExampleActivity.this.finish();
  }
} 

onCreateDialog(int) is only called the first time if you want to later influence the dialog to use the optional onPrepareDialog(int, Dialog) method.

1.2. ProgressDialog

Android also provides a ProgressDialog, which can be opened via a ProgressDialog.open() method call.

1.3. Custom dialogs

If you want to create your custom dialogs, you create a layout file for the dialog. This layout file is assigned to the dialog via the setContentView() method.

You would then use the dialog.findViewById() to find the elements in your layout and assign values to it.

The title of the dialog can be set via the setTitle() method.

2. Exercise: Displaying an alert dialog

2.1. Adding a dialog to your activity

The following demonstrates the usage of the AlertDialog dialog in an existing activity. An instance of this class can be created by the builder pattern, e.g., you can chain your method calls.

Ensure that the layout file of your activity contains a button with the android:onClick pointing to a method called onClick.

Change the code of your activity to the following.

// constant for identifying the dialog
private static final int DIALOG_ALERT = 10;

// existing code.....

// adjust this method if you have more than 
// one button pointing to this method
public void onClick(View view) {
  showDialog(DIALOG_ALERT);
}

@Override
protected Dialog onCreateDialog(int id) {
  switch (id) {
    case DIALOG_ALERT:
      Builder builder = new AlertDialog.Builder(this);
      builder.setMessage("This ends the activity");
      builder.setCancelable(true);
      builder.setPositiveButton("I agree", new OkOnClickListener());
      builder.setNegativeButton("No, no", new CancelOnClickListener());
      AlertDialog dialog = builder.create();
      dialog.show();
  }
  return super.onCreateDialog(id);
}

private final class CancelOnClickListener implements
    DialogInterface.OnClickListener {
  public void onClick(DialogInterface dialog, int which) {
    Toast.makeText(getApplicationContext(), "Cancle selected, activity continues",
        Toast.LENGTH_LONG).show();
  }
}

private final class OkOnClickListener implements
    DialogInterface.OnClickListener {
  public void onClick(DialogInterface dialog, int which) {
    AlertExampleActivity.this.finish();
  }
} 

2.2. Test dialog usage

If you run your application and click the corresponding button, your dialog is displayed.

Showing the running application with the dialog opened.

Tip

If you dialog is not displayed, ensure that the android:onClick property of your button points to the correct method and that the method signature is corrected entered as public void onClick (View view)).

3. Support free vogella tutorials

Maintaining high quality free online tutorials is a lot of work. Please support free tutorials by donating or by reporting typos and factual errors.

3.1. Thank you

Please consider a contribution if this article helped you.

Flattr this

3.2. Questions and Discussion

If you find errors in this tutorial, please notify me (see the top of the page). Please note that due to the high volume of feedback I receive, I cannot answer questions to your implementation. Ensure you have read the vogella FAQ as I don't respond to questions already answered there.

4. Links and Literature

4.1. Source Code

Source Code of Examples

4.2. Android Resources

Android Dialogs standard documentation.

4.3. vogella Resources

vogella Training Android and Eclipse Training from the vogella team

Android Tutorial Introduction to Android Programming

GWT Tutorial Program in Java, compile to JavaScript and HTML

Eclipse RCP Tutorial Create native applications in Java

JUnit Tutorial Test your application

Git Tutorial Put all your files in a distributed version control system