3

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?

Glenn
  • 12,741
  • 6
  • 47
  • 48

2 Answers2

9

We can use AlertDialog.getButton() so we can get button reference of AlertDialog. This should be called if the dialog is shown to user.

final EditText urlEditText = new EditText(this);


final AlertDialog dialog = new AlertDialog.Builder(this)
                    .setTitle("Enter the server URL here")
                    .setNegativeButton("Cancel", null)
                    .setPositiveButton("Ok",null)
                    .setView(urlEditText)
                    .create();
        dialog.setOnShowListener(new OnShowListener() {

            @Override
            public void onShow(DialogInterface arg0) {

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

                    @Override
                    public void onClick(View arg0) {

                        try
                        {
                            new URL(urlEditText.getText().toString());
                            Toast.makeText(getBaseContext(), "URL Accepted", 1).show();
                            dialog.dismiss();
                        }
                        catch (MalformedURLException e) {
                            Toast.makeText(getBaseContext(), "Invalid URL", 1).show();
                        }
                    }
                });
            }
        });

UPDATE 1: We can use AlertDialog.BUTTON1 but it is now deprecated.

Glenn
  • 12,741
  • 6
  • 47
  • 48
1

URLUtil provides useful methods like

isHttpUrl();
isValidUrl();

try to use one of them to validate your URL entered in EditText. And if URL is not valid, display alert one more time or don't dismiss dialog.

Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93