title: Is there a way prevent AlertDialog from closing with invalid inputs?
body: <p>I am using a simple text field alert dialog with a positive and a cancel button.  I want to validate my alert dialog and prevent the done button from closing the AlertDialog if the input is invalid.</p>
  
  <p>Is there any way short of creating a custom dialog to prevent the PositiveButton onClick() handler from closing the dialog if the validation fails?</p>
  
  <pre><code>class CreateNewCategoryAlertDialog {
      final EditText editText;
      final AlertDialog alertDialog;
  
      class PositiveButtonClickListener implements OnClickListener {
          @Override
          public void onClick(DialogInterface dialog, int which) {
              String name = editText.getText().toString();
              if(name.equals("")) {
                  editText.requestFocus();
                  editText.setError("Please enter a name");
                  // Some code to stop AlertDialog from closing goes here...
              } else {
                  doSomethingUsefulWithName();
              }
          }
      }
  
      AlertDialog buildAlertDialog(Context context) {
          return new AlertDialog.Builder(context)
          .setTitle(context.getString(R.string.enter_name))
          .setMessage(context.getString(R.string.enter_name_msg))
          .setView(editText)
          .setPositiveButton(context.getString(R.string.done), new PositiveButtonClickListener())
          .setNegativeButton(context.getString(R.string.cancel), null).create();
      }
  }
  </code></pre>