Monday, February 2, 2015

HOW TO ADD INPUT BOX IN A DISPLAYED ALERTDIALOG

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

dialogBox.setTitle("My App Name");
dialogBox.setMessage("Enter username");

// Adding EditText box into the dialog
final EditText etUsername = new EditText(this);
dialogBox.setView(input);
// you can set the further property of the EditText here 

dialogBox.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
  String username = etUsername.getText().toString();
// logic on the username value
  }
});

dialogBox.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int whichButton) {
     // logic if user click on the cancel button
  }
});

dialogBox.show();

No comments:

Post a Comment