I'm checking for invalid input to a group of edit texts in an alert dialog, by checking for null input and calling setError
. But in my current implementation the dialog still closes even though there has been invalid input.
A boolean check has been added to each edit text to prevent the dialog from being dismissed if any of the edit texts set the boolean to false like this:
else if(TextUtils.isEmpty(strColour)) {
colourText.setError("Please enter a value");
entriesValid = false;
` But the dialog is still dismissed despite the invalid input.
My question, whats the error here that allows the dialog to close on invalid input?
I set a break point on this line, if(entriesValid)
to check if the condition is triggered but it doesn't break here meaning that the check is be skipped.
This is the complete custom dialog class:
public class MyMessageDialog {
public interface MyMessageDialogListener {
public void onClosed(String ship, String scientist, String email, String volume, String color);
}
@SuppressLint("NewApi")
public static AlertDialog displayMessage(Context context, String title, String message, final MyMessageDialogListener listener){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater inflater = LayoutInflater.from(context);
builder.setTitle(title);
builder.setMessage(message);
final View layoutView = inflater.inflate(R.layout.custom_view, null);
builder.setView(layoutView);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
boolean entriesValid = true;
// get the edit text values here and pass them back via the listener
if(listener != null)
{
EditText shipText = (EditText)layoutView.findViewById(R.id.shipNameEditText);
EditText scientistNameText = (EditText)layoutView.findViewById(R.id.scientistEditText);
EditText scientistEmailText = (EditText)layoutView.findViewById(R.id.emailEditText);
EditText volumeText = (EditText)layoutView.findViewById(R.id.volumeEditText);
EditText colourText = (EditText)layoutView.findViewById(R.id.colourEditText);
listener.onClosed(shipText.getText().toString(),
scientistNameText.getText().toString(),
scientistEmailText.getText().toString(),
volumeText.getText().toString(),
colourText.getText().toString());
String strShipName = shipText.getText().toString();
String strScientistName = scientistNameText.getText().toString();
String strScientistEmail = scientistEmailText.getText().toString();
String strVolume = volumeText.getText().toString();
String strColour = colourText.getText().toString();
if(TextUtils.isEmpty(strShipName)) {
shipText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strShipName)) {
shipText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strScientistName)) {
scientistNameText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strScientistEmail)) {
scientistEmailText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strVolume)) {
volumeText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strColour)) {
colourText.setError("Please enter a value");
entriesValid = false;
}
}
if(entriesValid)
dialog.dismiss();
}
});
builder.show();
return builder.create();
}
}