2

I have a dialog box for the user to input an unlock code. I want to keep the box open if they input the wrong code so they have a chance to fix it.

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

    alert.setTitle("Unlock Code");
    alert.setMessage("Please enter the unlock code.");


    final EditText input = new EditText(this);
    alert.setView(input);

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
      String value = input.getText().toString();
     if(codeUnlock.checkCode(value)){ // checks the code that was put in
         Toast.makeText(MainActivity.this, "Thank you for purchasing.", Toast.LENGTH_LONG).show();
         yearPurchased = currentYear;
         checkForUpdate(false);

     }else{
         Toast.makeText(MainActivity.this, "Incorrect code.", Toast.LENGTH_LONG).show();


     }


      }
    });

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {

      }
    });

    alert.show();

basically, if checkCode is false i want to jsut display the toast but not close the window.

any suggestions?

Thank you.

Shmuel
  • 3,916
  • 2
  • 27
  • 45
  • you can't do that with an alert dialog. all the `set*Button` in AlertDialog will close the dialog. You need to use a Dialog with your own layout and buttons. – njzk2 Feb 27 '14 at 15:49
  • 1
    Have you tried setCancelable(false)? – Niko Feb 27 '14 at 15:51
  • 1
    possible duplicate of [Is it possible to make a button on an AlertDialog that doesn't automatically close the dialog?](http://stackoverflow.com/questions/2597371/is-it-possible-to-make-a-button-on-an-alertdialog-that-doesnt-automatically-clo) – njzk2 Feb 27 '14 at 15:51
  • 1
    why dont make custom dialog class, extend Dialog and simply call dismis() only when password is correct? – Darko Rodic Feb 27 '14 at 15:54
  • yah. all good suggestions. i was hoping for an easier way... – Shmuel Feb 27 '14 at 15:55
  • setCancelable(false) does not work – Shmuel Feb 27 '14 at 16:17

4 Answers4

1

Maybe this helps. Just add a onShowListener and you should be good. Haven't tested it myself but got it from here: https://stackoverflow.com/a/7636468. But it will only work on API 8+.

alert.setPositiveButton(android.R.string.ok, null);
alert.setOnShowListener(new DialogInterface.OnShowListener() {

@Override
public void onShow(DialogInterface dialog) {

    Button b = d.getButton(AlertDialog.BUTTON_POSITIVE);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // Do something

            // Dismiss once everything is OK.
            d.dismiss();
        }
    });
}
Community
  • 1
  • 1
Quitlox
  • 46
  • 5
1

you could try this:

builder.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
    if(!errorFlag) {
             // you need this flag in order to close the dialog 
             // when there is no issue
             dialog.dismiss();
     }
}
});

set the positive button:

builder.setOnShowListener(new OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
   Button b = builder.getButton(AlertDialog.BUTTON_POSITIVE);
       b.setOnClickListener(new View.OnClickListener() {

    @Override
public void onClick(View view) {
        // perform operations here
        // set the flag, that is isError = true or false
        // if false, call builder.dismiss();
    }
  }
});

set the negative button:

Button n = builder.getButton(AlertDialog.BUTTON_NEGATIVE);
n.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
        // perform operations here too if needed.
     }
    }
   });
  }
});

create the dialog like this:

final AlertDialog builder = new AlertDialog.Builder(YourActivity.this)
    .setNegativeButton("cancel", null)
    .setPositiveButton("ok", null)
    .create();

show the dialog using:

builder.show();
1

I ran into this problem yesterday and I solved it by overriding the click listener of the positive button:

AlertDialog.Builder builder = new AlertDialog.Builder(
            context);
    builder.setTitle(R.string.my_title);
    builder.setView(getLayoutInflater().inflate(
            R.layout.my_dialog, null));
    builder.setPositiveButton(android.R.string.ok, null);
    builder.setNegativeButton(android.R.string.cancel, null);
    final AlertDialog dialog = builder.create();
    dialog.show();
    dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(
            new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    EditText editTextUserId = (EditText) dialog
                            .findViewById(R.id.editTextUserId);
                    EditText editTextPassword = (EditText) dialog
                            .findViewById(R.id.editTextPassword);

                    if (editTextUserId.length() == 0
                            || editTextPassword.length() == 0) {
                        Toast.makeText(
                                context,
                                R.string.you_must_enter_username_and_password,
                                Toast.LENGTH_LONG).show();
                        return;
                    }

                    dialog.dismiss();
                }
            });
Santiago Aceñolaza
  • 276
  • 1
  • 2
  • 11
1

I know I'm super late to the party, but hopefully this helps people looking through Google. My solution requires that you throw an IOException if the user inputs something incorrect.

Flow Chart:

Flow Chart

This is how I did it:

User enters input into the dialog, then they press the positive button

builder.setPositiveButton("Enter", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        try {
            //do stuff if input was correct
            //if not, then throw an IOException
            dialog.dismiss();
            //dismiss, dialog goes away
        } catch (IOException e) {
            //User entered bad input, change the form to show feedback
            AlertDialog thisDialog = (AlertDialog) dialog;
            thisDialog.setTitle("Incorrect Format");
            thisDialog.setIcon(android.R.drawable.ic_dialog_alert);
            thisDialog.cancel();
            //cancel the dialog -> fire the OnCancelListener
        }
    }
});

Assuming it was incorrect, the OnCancelListener is fired

builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
    @Override
    public void onCancel(DialogInterface dialog) {
        ((AlertDialog) dialog).show();
    }
});

This brings us back to the positive button.

Just in case we want them to actually cancel the dialog and not go into an infinite loop, when they press the negative button, we'll dismiss the dialog there.

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

Really hope this helps!