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
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.
Android also provides a
ProgressDialog, which can be opened via a ProgressDialog.open() method call.
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.
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(); } }
Maintaining high quality free online tutorials is a lot of work. Please support free tutorials by donating or by 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.
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
No comments:
Post a Comment