0

I want my code to check if what is written in my dialog contains a certain string and if not to be unable to close the dialog. Now when I press the okay button the dialog disappears despite the fact the toast appears, so I guess I have done something wrong with setCancelable

             protected void showInputDialog() {
    // get prompts.xml view
    LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
    View promptView = layoutInflater.inflate(R.layout.url, null);

    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this, AlertDialog.THEME_HOLO_DARK);
    alertDialogBuilder.setView(promptView);

    final EditText editText = (EditText) promptView.findViewById(R.id.Button01);

    alertDialogBuilder.setTitle("Enter URL");
   // alertDialogBuilder.setCustomTitle("Enter URL");
   // alertDialogBuilder.setIcon(R.drawable.icon);

    alertDialogBuilder.setCancelable(false);

    alertDialogBuilder.setPositiveButton("DONE", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            String value = editText.getText().toString();

            if (value.contains("www.facebook.com/")) {
                //Toast.makeText(getBaseContext(), "You didn't enter the Name",Toast.LENGTH_SHORT).show();
               // alertDialogBuilder.setCancelable(false);
            }
            else {
                Toast.makeText(getBaseContext(), "You didn't enter the Name",Toast.LENGTH_SHORT).show();
                alertDialogBuilder.setCancelable(false);
            }
        }
    });
    // create an alert dialog
    AlertDialog alert = alertDialogBuilder.create();
    alert.show();
}

Any suggestions how to fix that?

edit: I have posted the whole function although I don't think it will help you

We're All Mad Here
  • 1,544
  • 3
  • 18
  • 46

2 Answers2

1

u can use onShowListener

dialog.setOnShowListener(new DialogInterface.OnShowListener() {

    @Override
    public void onShow(DialogInterface dialog) {

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

            @Override
            public void onClick(View view) {

                if (value.contains("www.stackoverflow.com/")) {
                   //do stuff
                   d.dismiss();         
                   }
                   else
                   {
                         Toast.makeText(getBaseContext(), "You didn't enter the Name                   correctly",Toast.LENGTH_SHORT).show();
                        alertDialogBuilder.setCancelable(false);
                       //Don't put dismiss() here 
                   }

            }
        });
    }
});

based on what i understood from your question this should work

mothana
  • 91
  • 2
  • 4
  • 12
1

It is not related to setCancelable, which only disallow cancel by user, but not disable cancel by code.

By default, AlertDialog will close on any button click, but there is a workaround.

Actually @mothana is talking about the same workaround, I try to put it together with your code.

final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this, AlertDialog.THEME_HOLO_DARK);
alertDialogBuilder.setView(promptView);

final EditText editText = (EditText) promptView.findViewById(R.id.Button01);

alertDialogBuilder.setTitle("Enter URL");

alertDialogBuilder.setCancelable(false);

alertDialogBuilder.setPositiveButton("DONE", null); //part of the workaround
// create an alert dialog
final AlertDialog alert = alertDialogBuilder.create();

// workaround
alert.setOnShowListener(new DialogInterface.OnShowListener() {

    @Override
    public void onShow(DialogInterface dialog) {

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

            @Override
            public void onClick(View view) {

                if (value.contains("www.stackoverflow.com/")) {
                   //you code here
                   alert.dismiss();         
                }
                else
                {
                     Toast.makeText(getBaseContext(), "You didn't enter the Name",Toast.LENGTH_SHORT).show();
                     //Don't put dismiss() here 
                }

            }
        });
    }
});
alert.show();

You can check the reference here How to prevent a dialog from closing when a button is clicked

Community
  • 1
  • 1
Derek Fung
  • 8,171
  • 1
  • 25
  • 28