Question

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?

Was it helpful?

Solution

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top