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.