Labels

Tuesday, June 28, 2011

Prompt Dialog for Android

Reference: Prompt User Input with an AlertDialog

I've written a helper class, that makes it easy and fast to create prompt-dialogs on Android.
Usage:
PromptDialog dlg = new PromptDialog(MainActivity.this, R.string.title, R.string.enter_comment) {
 @Override
 public boolean onOkClicked(String input) {
  // do something
  return true; // true = close dialog
 }
};
dlg.show();

You just have to create a new class PromptDialog:
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.widget.EditText;

/**
 * helper for Prompt-Dialog creation
 */
public abstract class PromptDialog extends AlertDialog.Builder implements OnClickListener {
 private final EditText input;

 /**
  * @param context
  * @param title resource id
  * @param message resource id
  */
 public PromptDialog(Context context, int title, int message) {
  super(context);
  setTitle(title);
  setMessage(message);

  input = new EditText(context);
  setView(input);

  setPositiveButton(R.string.ok, this);
  setNegativeButton(R.string.cancel, this);
 }

 /**
  * will be called when "cancel" pressed.
  * closes the dialog.
  * can be overridden.
  * @param dialog
  */
 public void onCancelClicked(DialogInterface dialog) {
  dialog.dismiss();
 }

 @Override
 public void onClick(DialogInterface dialog, int which) {
  if (which == DialogInterface.BUTTON_POSITIVE) {
   if (onOkClicked(input.getText().toString())) {
    dialog.dismiss();
   }
  } else {
   onCancelClicked(dialog);
  }
 }

 /**
  * called when "ok" pressed.
  * @param input
  * @return true, if the dialog should be closed. false, if not.
  */
 abstract public boolean onOkClicked(String input);
}

21 comments:

  1. Is there some missing code beyond line 57?

    ReplyDelete
  2. @Epyx
    no, since this class is abstract the method onOkClciked have not to be implemented there.

    ReplyDelete
  3. Thanks a lot for this! Clean and simple. Me gusta :)

    ReplyDelete
  4. @slampy
    then you have to create a own layout

    ReplyDelete
  5. hello,

    Nice code, but i get an error message on line 26, 27

    setPositiveButton(R.string.ok, this); ok not found
    setNegativeButton(R.string.cancel, this); cancel not found

    Best regards

    ReplyDelete
    Replies
    1. you have to define ok and cancel in your res/values xml file. And import the correct R class (not android.R)!

      Delete
    2. Thanks a lot for your helps.
      It's work fine.

      I'm beginner an Android dev.

      Delete
  6. I've been trying to use your code in an application that has multiple popups, however, your code doesn't seem to include a way to stop the onCreate method from running. This is causing the popups to render on top of each other. Is there any way around this?

    ReplyDelete
    Replies
    1. of course it doesn't! The onCreate method must not block the ui-thread.
      If you want to display multiple dialogs one after another you have to open the next dialog on the onClicked method.

      Delete
  7. This comment has been removed by the author.

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. Thanks for the code.great work! How to wait the dialog if the user has not input anything and press OK button. Here dialog closes even if the user has input something or not.pls reply

    ReplyDelete
    Replies
    1. you have the opportunity to return false in the onOkClicked method to avoid that the dialog closes.

      Delete
    2. // Show name input dialog
      PromptDialog d = new PromptDialog(this, R.string.newSessionDialogTitle,
      R.string.newSessionDialogMessage) {

      public boolean onOkClicked(String input) {
      // do something
      Log.d(TAG, input);
      if (input.isEmpty()) {
      return false;
      }
      SessionName = input;
      return true; // true = close dialog
      }
      @Override
      public void onCancelClicked(DialogInterface dialog) {
      dialog.dismiss();
      finish();
      }
      };
      d.show();

      My dialog was dismissed even when I returned false. Am I doing it wrong?

      Delete
  10. Hey
    I m getting error in dlg.show();[Syntax error on token "show",identifier expected after this token]
    Pls rply

    ReplyDelete
  11. This is awesome! Thanks! Very detailed!

    ReplyDelete
  12. My dialog was dismissed even though I returned false as well :^(

    ReplyDelete
  13. This is very helpful, but there seems to be something missing: How do you get the value that was entered out of the Alert Dialog? It returns a boolean for OK/Cancel.

    ReplyDelete
  14. i was really a great tutorials!!!
    Hats Off!!

    ReplyDelete