Cannot setError inside a dialog box that closes by itself

As a headline, I am encountering problems setting error inside EditText mEditText.

 private AlertDialog.Builder buildDialog(String mailString)
  {
    final AlertDialog.Builder alertDialogBuilder =
      new AlertDialog.Builder(LoginActivity.this);

    alertDialogBuilder.setTitle("Insert mail");
    alertDialogBuilder.setMessage("email");
    // Set an EditText view to get user input
    mMailEditText = new EditText(LoginActivity.this);
    if (mailString != null)
      mMailEditText.setText(mailString);
    alertDialogBuilder.setView(mMailEditText);
    alertDialogBuilder.setPositiveButton(
      "Ok", new DialogInterface.OnClickListener()
      {
        public void onClick(DialogInterface dialog, int whichButton)
        {
          String email = mMailEditText.getText().toString();
          if (!TextUtils.isEmpty(email) && !isEmailValid(email))
          {
             mMailEditText.setError(getString(
                R.string.activity_login_error_invalid_email));
          }
          else
          {
            attemptLoginOrRegister(UserTasks.REGISTER, email);
          }
        }
      });
    alertDialogBuilder.setNegativeButton(
      "Cancel", new DialogInterface.OnClickListener()
      {
        public void onClick(DialogInterface dialog, int whichButton)
        {
          dialog.cancel();
        }
      });

    mAlertDialog = alertDialogBuilder.create();
    return alertDialogBuilder;
  }

  private void setListeners()
  {
    mRegisterButton.setOnClickListener(
      new View.OnClickListener()
      {
        @Override
        public void onClick(View v)
        {
          buildDialog(null).show();
        }
      });
  }

setListeners()always called from onCreate, and buildDialogcalled when the user clicks on the registration button. Debugging the application, the line containing mMailEditText.setError(), is performed correctly if the message is invalid, but the error message is not displayed, and the dialog just closes. What is wrong with my approach?

EDIT: if you want, here is a simplified version of the class code that does not require any external library. I also added layout and line files.

+4
1

, . :

   View view = getLayoutInflater().inflate(R.layout.custom_dialog, null);

   final EditText editTextEmail = (EditText) view.findViewById(R.id.editCategory);

   final AlertDialog alertDialog = new AlertDialog.Builder(LoginActivity.this)
            .setView(view)
            .setPositiveButton(R.string.str_ok, null)
            .setNegativeButton(R.string.str_cancel, null)
            .create();

    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {

            Button buttonPositive = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE);
            buttonPositive.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                 // Do whatever you want when positive button is clicked
            });

            Button buttonNegative = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_NEGATIVE);
            buttonNegative.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
// Do whatever you want when negative button is clicked
                }
            });
        }
    });

    alertDialog.show();

XML-, EditText. setView AlertDialog. setOnShowListener AlertDialog. , . .

+4

All Articles