Sean
Sean

Reputation: 141

Prevent AlertDialog from closing when EditText is empty

So without having to create a custom dialog as I currently have a number of dialog layouts and don't want to have to do it for each one, is there a way to prevent this dialog from closing when the positive button is pressed and the EditText is empty?

Currently it closes the dialog every time I hit enter and there is nothing in the EditText field.

public AlertDialog webpageDialog() {
    AlertDialog.Builder webpageDialogBuilder = new AlertDialog.Builder(context);
    LayoutInflater inflater = LayoutInflater.from(context);
    webpageDialogBuilder.setView(inflater.inflate(R.layout.dialog_webpage, null))
            .setNegativeButton(context.getString(R.string.cancel), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    final AlertDialog webpageDialog = webpageDialogBuilder.create();

    webpageDialog.setButton(DialogInterface.BUTTON_POSITIVE, context.getString(R.string.enter), new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            EditText webpageInput = (EditText) webpageDialog.findViewById(R.id.dw_et_webpage_address);
            Log.d(TAG, "Positive on click");
            if(TextUtils.isEmpty(webpageInput.getText().toString())){
                Log.d(TAG, "Edit text empty");
                webpageInput.setError(context.getString(R.string.error_web_required));
            } else {
                Log.d(TAG, "Edit text not empty");
                ms.setUriString("http://" + webpageInput.getText().toString());
                ms.returnWithResult(1);
                dialog.cancel();
            }
            Log.d(TAG, "Returning");
        }
    });
    Log.d(TAG, "Returning dialog");
    return webpageDialog;
}

Upvotes: 0

Views: 372

Answers (2)

Aashish Aadarsh
Aashish Aadarsh

Reputation: 491

I hope following line can help,

webpageDialogBuilder.setCancelable(false);

Upvotes: 0

Michael Angelo Reyes
Michael Angelo Reyes

Reputation: 130

this is how I'm doing it.

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);

        EditText yourEditText = new EditText(this);
        layout.addView(yourEditText);

        builder.setView(layout);

        // Set up the buttons
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        final AlertDialog dialog = builder.create();

        dialog.show();

        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if ("".equals(yourEditText.getText().toString().trim())) {
                    //this will stop your dialog from closing
                    yourEditText.setError("This field is required!");
                    return;

                }

                //you logic here


                dialog.dismiss();

            }
        });

Upvotes: 1

Related Questions