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();
21 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...
You would need to add all three EditText Views into a single View, and then add the single view to the dialog.
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
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;
}
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;
}
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
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
Thank you! Very helpful!
Thank you very much!
Add input.getText().toString();
Read here.. Android Examples