The subject kinda says it all.. I'm requesting a PIN code from the user, if they enter it, click the OK Positive Button and the PIN is incorrect I want to display a Toast but keep the dialog open. At the moment it closes automatically.. Sure this is very trivial thing to correct but can't find the answer yet.

Thanks, so dialogs, unless you specify a custom layout will automatically close in an onclick?
– DaveOct 25 '10 at 17:20

I do belive so, i might be mistaken, but I found it easier to do a custom dialog instead of trying to get another one to work and I had total control over what it looks like and how it behaves. Plz mark the answer as accepted if it works for you.
– blindstuffOct 25 '10 at 17:26

You do not need to create a custom class. You can register a View.OnClickListener for the AlertDialog. This listener will not dismiss the AlertDialog. The trick here is that you need to register the listener after the dialog has been shown, but it can neatly be done inside an OnShowListener. You can use an accessory boolean variable to check if this has already been done so that it will only be done once:

This solution worked for me, thanks
– Yulia RogovayaOct 26 '11 at 15:43

1

Unfortunately, setOnShowListener is only available in API Level 8 and later. To support API 7, I'm following the approach provided in the page you linked to: pass a no-op listener to setNegativeButton, call show(), then get a reference to the button and call setOnClickListener() with the full listener.
– TravisFeb 5 '12 at 4:57

+1 for nice and helpful answer, This solution works for me.
– rajparaOct 3 '12 at 6:19

Nice work. Just one error in the code: alertAndHelp should be alert. Also, it's not entirely clear to me why you need the alertReady flag, is it just to avoid running the code again when the same alertdialog is show again?
– MatthiasAug 12 '15 at 12:19

Thanks for pointing the variable name mistake out! (That's fixed now.) The alertReady flag is there just to make explicit that it's not necessary to run the code inside button.setOnClickListener every time the dialog is shown, in case somebody ends up doing something laborous in there. :)
– zenperttuSep 14 '15 at 19:53

This is what I was doing however the dialog automatically closes even without the dismiss() call.
– DaveOct 25 '10 at 17:19

Okay, after looking into it, it looks like custom dialog is going to be your best bet.. I'll edit my answer to one that you'll find useful. I'm actually using it in my application. dont forget to mark as accepted if you use my answer :)
– SamuelOct 26 '10 at 3:52