4

As the title, I'm facing issues while setting an 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() get always called from onCreate, while buildDialog is called when the user perfroms a click on the register button. Debugging the app, the line containing mMailEditText.setError() is correctly executed if the email isn't valid, but the error message isn't displayed and the dialog simply closes. What is wrong with my approach?

EDIT: if you want, here is a simplified version of the class code that doesn't require any external library. I've also added layout and strings files.

tigerjack
  • 1,158
  • 3
  • 21
  • 39
  • are u sure mMailEditText.setError( is called? – KOTIOS Jan 28 '15 at 10:04
  • sure, it always get called when the condition is true. – tigerjack Jan 28 '15 at 10:08
  • use getResources().getString(R.string.error_invalid_email); – KOTIOS Jan 28 '15 at 10:11
  • I've also tried to split the `getString()` part from the `setError()` part; the string is correctly retrieved and the error correctly assigned to the `EditText`. Simply, it doesn't show anything and the dialog get closed @diva – tigerjack Jan 28 '15 at 10:11
  • @diva it doesn't work even using `getResources()`. Btw, pretty new to chats, I'll be here in a few moments – tigerjack Jan 28 '15 at 10:17
  • 1
    possible duplicate of [How to prevent a dialog from closing when a button is clicked](http://stackoverflow.com/questions/2620444/how-to-prevent-a-dialog-from-closing-when-a-button-is-clicked) – Hemant Shori Jul 09 '15 at 06:28

1 Answers1

4

You have gone for the right approach although you need to change your code. Please take a look at the following code:

   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();

Create an XML layout including the EditText you want. Inflate the layout in a view and pass that view in setView method of AlertDialog's instance. Simply override setOnShowListener using AlertDialog's instance. Thereafter, retrieve the positive and negative buttons as shown above. That's it.

reflective_mind
  • 1,475
  • 3
  • 15
  • 28
Suraj Makhija
  • 1,376
  • 8
  • 16