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".
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();
13 Comments
Matter of style but you also can inline the whole thing
Cheers very helpful. I like the inline version but found it bloats my code when I use auto formating..
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
this is awesome!!! I spent the whole night looking for a code like this. Thank you.
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 ?
Why am I getting an error on
String value = input.getText();? Eclipse wants it to return an Editable or CharSequence...Change it to:
Thanks! Its very helpful.
This code snippet is a LIFESAVER!! How will i get back the 4 days i lost in inflater h3ll? This was so simple.
Thank you for submiting this entry. Keep it up!!
saint petersburg hotels
I guess there's something I must miss. Im trying to get this to work for several hours:
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"andandroid:id="@+id/et_password"So the only thing that could throw is the assignment of the strings of the EditText!? Any suggestions? Help appreciated :)
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...
I've written a helper class that makes it easy to create a prompt dialog with only a few lines of code.
See full code => Prompt Dialog for Android