We can perform a validation by adding a positive button with implementation
final EditText urlEditText = new EditText(this);
DialogInterface.OnClickListener event = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
try
{
// Validate URL
new URL(urlEditText.getText().toString());
Toast.makeText(getBaseContext(), "URL Accepted", 1).show();
}
catch (MalformedURLException e) {
Toast.makeText(getBaseContext(), "Invalid URL", 1).show();
}
}
};
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Enter the server URL here")
.setNegativeButton("Cancel", null)
.setPositiveButton("Ok",event)
.setView(urlEditText)
.create();
dialog.show();
If we enter a wrong URL it will be validated and display a wrong URL Toast
. However, the dialog is dismissed it should be still displayed until the user press "Cancel" or enter the correct URL. There are solutions around here like recreating the dialog and display it to user again but this would be a good idea.
Is there a way that If we enter a wrong URL the AlertDialog
still there?