Dialog box under Android
With Java / Swing, each showXxxDialog method in the JOptionPane class creates a modal dialog box waiting for a user action. It’s modal because the user can’t do any action outside this one. Let’s take the following example :
int response = JOptionPane.showConfirmDialog(null,
"Do you want to delete this file ?",
"Confirmation",
JOptionPane.YES_OPTION);
if (response == JOptionPane.YES_NO_OPTION) {
// ... action if response = Yes
}
We can see the call is synchronous, which means the method returns a value that allow to process the response immediately.
Under Android, dialog boxes are also modals. But the processing of the response must be done asynchronously.
So, one must use a different programming strategy, based on callback. In the example of a Yes/No dialog box, a callback for the Yes answer is needed and eventually another one for the No answer. The code for Android looks like this :
Activity activity = .....; // find the current Activity
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage("Do you want to delete this file ?");
// definition of the callback for the Yes answer
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// ... action if answer = Yes
}
});
builder.show(); // doesn't return user's choice
// after this line, the user may not have already answered
// to the question (because the call is asynchronous)
In order to make the code a bit clearer, I create an abstract class called Command :
abstract public class Command implements DialogInterface.OnClickListener {
@Override
public final void onClick(DialogInterface dialog, int which) {
execute();
dialog.dismiss();
}
abstract public void execute();
}
I also write the following method :
public void displayYesNoDialog(Activity context, int messageId,
Command yesCommand) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(context.getString(messageId));
builder.setPositiveButton("Yes", yesCommand);
builder.show();
}
I use it like that :
Activity activity = .....; // find the current Activity
int messageId = .....; // find the message identifier
Command yesCommand = new Command() {
public void execute() {
// ... action if answer = Yes
}
displayYesNoDialog(activity, messageId, yesCommand);
![[del.icio.us]](http://www.duminy.fr/blog/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://www.duminy.fr/blog/wp-content/plugins/bookmarkify/digg.png)
![[Google]](http://www.duminy.fr/blog/wp-content/plugins/bookmarkify/google.png)
![[LinkedIn]](http://www.duminy.fr/blog/wp-content/plugins/bookmarkify/linkedin.png)
![[StumbleUpon]](http://www.duminy.fr/blog/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Windows Live]](http://www.duminy.fr/blog/wp-content/plugins/bookmarkify/windowslive.png)
![[Yahoo!]](http://www.duminy.fr/blog/wp-content/plugins/bookmarkify/yahoo.png)
![[Email]](http://www.duminy.fr/blog/wp-content/plugins/bookmarkify/email.png)





