Android Dialog(1) – 2 Supplement

This article is a supplement of  Android Dialog (1).

In a place that explains how to add buttons on a dialog, I have introduced the only example of using the DialogFragment. Therefore, I will introduce the following example using the Activity.

If you use the DialogFragment, you place the listener and onCreateDialog method in it. On the other hand, if you do not use DialogFragment, you may place both in the Activity.

getActivity() is not a method of Activity. Therefore, I replaced it to this (because of the reference to the outer class from an anonymous class, MainActivity.this).

[Related Articles]
Android Dialog (1)
Android Dialog(2) -Custom Dialog-

Android Dialog (1)

I recently dealt with the dialogs in an application and had a lot trial and error. So I put together the record.

First, A dialog is a small window. By dialogs, you can alert the user or prompt the user for simple input, etc. You can also think that the Dialog is the small Activity. However, there is no inheritance relationship between the class Dialog and Activity.

So, we will look at the actual code.

For example, the code that creates an instance of the AlertDialog, which is a kind of ready-made Dialog, is something like the following.

You must add “return dialog” in the above code, and write them in onCreateDialog method. However, to implement onCreateDialog, you can select the location of the following two.

  1. You can override the onCreateDialog method of Activity.
  2. You can override the onCreateDialog method of DialogFragment.

Approach “1” is the way that can fit the old version of Android. But it is now deprecated.
On the other hand, the second method is available only since API Level 15 (Android Version 4.0.3). However, if you use the support library, it is available since API Level 4 (Android Version 1.6). Therefore, the second method is recommended now.

I will show the code examples in each case.

To make it easier to compare them, I define the method MainActivity.showAlert() that show alert. And as shown below, I call showAlert() in MainActivity.onCreate.

[Example 1]
If you are using Activity.onCreateDialog

To show dialog, you may just call the method Activity.showDialog. Then, Activity.onCreateDialog is called, dialog is generated, and it is shown.

In addition, the argument of AlertDialog.Builder is the context to display the dialog. Although in DialogFragment I will use getActivity() to get it, here I pass this because the Activity is inherited from the Context.

[Example 2]
If you are using DialogFragment.onCreateDialog

As described in the above-mentioned showAlert, to display the dialog, you instantiate the (derived class of) DialogFragment, and call the show method. At that time, you need to give FragmentManager as the argument.

The above code is an example of using the support library. Therefore, compared with if you do not use the support library(, if only for the more than Android 4.0.3), I made the following modifications. (For more information, please refer to Using the Android Support Library)

  1. Instead of Activity, use the derived class of FragmentActivity
  2. Instead of getFragmentManager, use the method getSupportFragmentManager to get FragmentManager

On both [Examples], a dialog will appear as follows. When you press the “Back” button, you will return to the previous activity.

Alert Dialog

You can add buttons by using the methods of the AlertDialog.Builder.

As a dummy, here, we offer the following listeners.(As R.string.ok, R.string.cancel, I have defined a string to be displayed on the button.)

You can set the buttons by setPositive and NegativeButton method.

You will come to the following dialog.

Alert Dialog with Button
This time, I used the ready-made dialog AlertDialog. Next time, I will summarize the custom dialog.

[Related Articles]
Android Support Library
Using the Android Support Library
Android Dialog(1) – 2 Suppliment
Android Dialog(2) -Custom Dialog-