Prompt User Input with an AlertDialog

8 votes · 20 comments

This code creates an input-dialog with AlertDialog.Builder where a user can enter text in an EditText field and press on "Ok" and "Cancel".

raw ·
copy
· download
AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setTitle("Title"); alert.setMessage("Message"); // Set an EditText view to get user input final EditText input = new EditText(this); alert.setView(input); alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String value = input.getText(); // Do something with value! } }); alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // Canceled. } }); alert.show();
Add a comment

20 Comments

Matter of style but you also can inline the whole thing

new AlertDialog.Builder(Main.this)
    .setTitle("Update Status")
    .setMessage(message)
    .setView(input)
    .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            Editable value = input.getText(); 
        }
    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            // Do nothing.
        }
    }).show();
Reply · June 5, 2009, 11:41 a.m.

Cheers very helpful. I like the inline version but found it bloats my code when I use auto formating..

Reply · Aug. 29, 2009, 9:54 p.m.

There is a simple work around to avoid it auto-formatting. You can put a comment at the end of each line (and provide extra info if you want) but it will stop Eclipse from making it into one single line

Reply · Oct. 27, 2011, 11:59 a.m.

this is awesome!!! I spent the whole night looking for a code like this. Thank you.

Reply · Sept. 23, 2009, 8:48 a.m.

how do i display the user input on emulator? is this the only way in android java to get user input ? how to copy the input text to buffer or write to a file ?

Reply · Sept. 30, 2009, 8:29 p.m.

Why am I getting an error on String value = input.getText(); ? Eclipse wants it to return an Editable or CharSequence...

Reply · March 22, 2011, 3:50 a.m.

Change it to:

String value = input.getText().toString();
Reply · March 22, 2011, 5:23 p.m.

Thanks! Its very helpful.

Reply · April 7, 2011, 5:31 a.m.

This code snippet is a LIFESAVER!! How will i get back the 4 days i lost in inflater h3ll? This was so simple.

Reply · April 13, 2011, 6:43 p.m.

Thank you for submiting this entry. Keep it up!!

saint petersburg hotels

Reply · April 24, 2011, 8:48 p.m.

I guess there's something I must miss. Im trying to get this to work for several hours:

Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.login_dialog, (ViewGroup) findViewById(R.id.rl));

new AlertDialog.Builder(this) .setView(layout) .setTitle("Login") .setPositiveButton("Login", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { EditText email_input = (EditText)findViewById(R.id.et_email); EditText pwd_input = (EditText)findViewById(R.id.et_password); try { m_email = email_input.getText().toString(); m_password = pwd_input.getText().toString(); Toast.makeText(CLogin.this, m_email+ " || "+ m_password, Toast.LENGTH_LONG).show(); } catch (NullPointerException e) { System.out.println(">>> getText(): "+e); } } }).show();

When I click the Button I get a NullPointerException thrown, but I dont know why. The IDs of the EditTexts are right:

android:id="@+id/et_email" and android:id="@+id/et_password"

So the only thing that could throw is the assignment of the strings of the EditText!? Any suggestions? Help appreciated :)

Reply · May 6, 2011, 2:32 p.m.

I am trying to add user name and password field, but only one of the field is visible. How do I overcome this??

Thanks in advance...

    AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle("Registration");

        alert.setMessage("Server Name:");
        // Set an EditText view to get user input 
        final EditText servername = new EditText(this);
        alert.setView(servername);

        alert.setMessage("User Name:");
        // Set an EditText view to get user input 
        final EditText username = new EditText(this);
        alert.setView(username);

        alert.setMessage("Password:");
        // Set an EditText view to get user input 
        final EditText password = new EditText(this);
        //alert.setView(password);

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
          String value1 = servername.getText().toString();
          String value2 = username.getText().toString();
          String value3 = password.getText().toString();
          // Do something with value!
          }
        });
Reply · June 27, 2011, 12:24 p.m.

You would need to add all three EditText Views into a single View, and then add the single view to the dialog.

Reply · July 13, 2012, 5:41 p.m.

I've written a helper class that makes it easy to create a prompt dialog with only a few lines of code.

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();

See full code => Prompt Dialog for Android

Reply · June 28, 2011, 1:36 p.m.

import android.R; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.text.Editable; import android.widget.EditText;

/ * Creates a dialog with an input for text entry. */ public class InputDialog { public interface OnInputClickListener { public void onInputClickOk(String text); public void onInputClickCancel(String text); } OnInputClickListener mOnInputClickListener; public void setOnButtonClickOkListener(OnInputClickListener instance) { mOnInputClickListener = instance; } Context context; String title, message; EditText input;

public InputDialog(Context context, String title, String message) {
    this.context = context;
    this.title = title;
    this.message = message;
}
private String getText() {
    Editable editable = input.getText();
    return editable == null ? "": editable.toString();
}
public void show() {
    input = new EditText(context);
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setTitle(title);
    alert.setMessage(message);

    // Set an EditText view to get user input
    alert.setView(input);

    alert.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            mOnInputClickListener.onInputClickOk(getText());
        }
    });
    alert.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            mOnInputClickListener.onInputClickOk(getText());
        }
    });
    alert.show();
}

}

Reply · Aug. 17, 2013, 10:56 p.m.

import android.R; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.text.Editable; import android.widget.EditText;

/ * Creates a dialog with an input for text entry. */ public class InputDialog { public interface OnInputClickListener { public void onInputClickOk(String text); public void onInputClickCancel(String text); } OnInputClickListener mOnInputClickListener; public void setOnButtonClickOkListener(OnInputClickListener instance) { mOnInputClickListener = instance; } Context context; String title, message; EditText input;

public InputDialog(Context context, String title, String message) {
    this.context = context;
    this.title = title;
    this.message = message;
}
private String getText() {
    Editable editable = input.getText();
    return editable == null ? "": editable.toString();
}
public void show() {
    input = new EditText(context);
    AlertDialog.Builder alert = new AlertDialog.Builder(context);

    alert.setTitle(title);
    alert.setMessage(message);

    // Set an EditText view to get user input
    alert.setView(input);

    alert.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            mOnInputClickListener.onInputClickOk(getText());
        }
    });
    alert.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            mOnInputClickListener.onInputClickCancel(getText());
        }
    });
    alert.show();
}

}

Reply · Aug. 17, 2013, 11:02 p.m.

very nice code. a question, how can i make the edittext as a numeric inputtype ? so as to display a numeric pad instead of a full keyboard

Reply · Feb. 18, 2014, 3:13 p.m.

Hi, you should change the XML code for your component in your layout, in fact, you should use this: android:inputType="numberDecimal" This will show you the numeric keyboard

Regards

Reply · June 16, 2014, 2:44 a.m.

Thank you! Very helpful!

Reply · March 11, 2014, 4:04 p.m.

Thank you very much!

Reply · Oct. 25, 2014, 10:18 a.m.