Home » Android » android – Validating Dialog Input

android – Validating Dialog Input

Posted by: admin June 15, 2020 Leave a comment


I am using an AlertDialog that is very simple, just a custom view with a text box, and the alertdialog positive submit button. I would like to validate that the text box has had text entered before the user dismisses the dialog. I see two possibilities, with questions about each:

  • Disable the submit button until the
    text box is not empty (in some
    onChange() type handler for the text

    • How do you know when the content of the text box changes?
    • How do you get a reference to the AlertDialog’s button object?
  • Check in the submit button’s
    onClick() and cancel the dismissing
    of the dialog if it’s empty.

    • Is it possible to do this with an AlertDialog’s button? The dialog dismisses without manually calling dismiss() or cancel(), so I’m not sure…

Is either of these options possible with an AlertDialog (vs a custom dialog)?

I think the second option would be the simplest, but I’m up for either if it’s possible.

How to&Answers:

The easiest way i think is to defined your own dialog in a xml file. Then you can display it very simple (in this example somewhere in the activity class):

Dialog dialog = new Dialog(this);

Button yourButton = dialog.findViewById(R.id.yourButton);
final EditText text = dialog.findViewById(R.id.yourTextEdit);
yourButton.setOnClickListener( {

public void onClick(View view) {
   if ( ! (text.getText().toString.equals(""))) {
        //go on here and dismiss dialog



I set a TextWatcher to an input field, and then enable the positive button when validation conditions are met. I disable the positive button by default.

AlertDialog.Builder builder = new AlertDialog.Builder(getSherlockActivity());
final View dialogView = LayoutInflater.from(getSherlockActivity()).inflate(R.layout.create_playlist, null);
final EditText inputField = (EditText) dialogView.findViewById(R.id.edit_newPlaylistName);
... //proceed to setup dialog using builder
final AlertDialog alertDialog = builder.show();
final Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
inputField.addTextChangedListener(new TextWatcher() {
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // my validation condition
        if (inputField.getText().length() > 0) {
        } else {

    public void afterTextChanged(Editable s) {



Here’s a way to validate input and keep an Alert dialog on the screen if the input is not valid. Actually it removes the dialog and puts up a new copy of it.

In your setPostiveButton’s onClick function, do your validation.
If things are not as they should be, show the user a Toast.
Then call removeDialog on your dialog.
Then – and this is the tricky part, asynchronously call showDialog on your dialog (with args if applicable).
Also: In order not to lose the user’s input, you should put the values that they entered into the bundle which you invoke the dialog with. And of course your setup code for the dialog needs to look for those values in the bundle and prepopulate the dialog fields appropriately.

So your code would look something like this:

new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) 
    if ((your validation conditions)) {
        // validation conditions not satisfied
        Toast.makeText(blah blah).show();
        // now create a new args bundle for the dialog
        Bundle newArgs = new Bundle();
        // now copy whatever you need from the args used to invoke to dialog
        newArgs.putIntegerArrayList("items", myList);
        // now save the user's input in a bundle
        newArgs.putString("dialogToFieldContent", toString);
        final Bundle finalArgs = newArgs;
        Handler handler = new Handler();
        handler.post(new Runnable() {
            public void run() {
            showDialog(myDialogId, finalArgs);
    else {
        // if everything is ok