Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Built-in single input text field dialog for Android

Does Android come with a dialog that I can pop up that shows a single input text field with an OK and Cancel button or do I have to create that myself?

like image 358
Johann Avatar asked Mar 02 '14 10:03

Johann


1 Answers

No, there is no such component available but we can manage to achieve this by using setView(View) without creating any xml in layout/inflate. As per the code below:

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().toString();
        Log.d("", "Pin Value : " + value);
        return;
    }
});

alert.setNegativeButton("Cancel",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            return;
        }
});
alert.show();
like image 75
Manmohan Badaya Avatar answered Sep 22 '22 21:09

Manmohan Badaya