Support free tutorials

vogella training Training Books



Using dialogs in Android programming - Tutorial

Lars Vogel

Version 9.9

27.12.2012

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. About this website
3.1. Donate to support free tutorials
3.2. Questions and discussion
3.3. License for this tutorial and its code
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. About this website

3.1. Donate to support free tutorials

Please consider a contribution Support free tutorials if this article helped you. It will help to maintain our content and our Open Source activities.

3.2. Questions and discussion

Writing and updating these tutorials is a lot of work. If this free community service was helpful, you can support the cause by giving a tip as well as reporting typos and factual errors.

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.

3.3. License for this tutorial and its code

This tutorial is Open Content under the CC BY-NC-SA 3.0 DE license. Source code in this tutorial is distributed under the Eclipse Public License. See the vogella License page for details on the terms of reuse.

4. Links and Literature

4.1. Source Code

Source Code of Examples

4.2. Android Resources

Android Dialogs standard documentation.

4.3. vogella Resources

TRAINING SERVICE & SUPPORT
The vogella company provides comprehensive training and education services from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring. We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience what many before you refer to as “The best IT class I have ever attended”. The vogella company offers expert consulting services, development support and coaching. Our customers range from Fortune 100 corporations to individual developers.