1

Is there some way to do validation upon clicking ok. Im not dismissing it but it closes when I click Ok. I have written a custom AlertDialog that uses some edit texts. Problem is I can't validate anything. If validation fails I want to change message and tell user but instead it just closes. I'm using AlertBuilder

        builder.setPositiveButton(DialogInterface.OnClickListener(){
               @Override
                public void onClick(DialogInterface dialog, int which) {
                  // I don't dismiss here.
                }

           }
Code Droid
  • 10,344
  • 17
  • 72
  • 112

2 Answers2

3

You can do this by overriding the onClickListener. The trick is to get the button after create and showing the dialog.

//  Create you dialog here and show it
...
dialog.show();

Button positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
positiveButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View onClick) {
        //  Valid checking
        ...

        if (valid) {
            dialog.dismiss();
        } else {
            //  Not valid
        }

    }
});
Hanon
  • 3,917
  • 2
  • 25
  • 29
-1

if you write nothing inside onClick method definately it will dismiss whether you want or not, then why you asking this question as you have not write anything inside that method. as It is alertdialog it will get closed and if you dont want it close then use setNeutralButton instead of setPositiveButton. It wont close if you use neutral button

mayank_droid
  • 1,015
  • 10
  • 19
  • 1
    setNeutralButton also closes automatically – Code Droid May 30 '12 at 06:36
  • Definitely not enough to just use neutral. – Code Droid May 30 '12 at 06:40
  • then dont use Alert Dialog, instead make your own custom dialog – mayank_droid May 30 '12 at 08:43
  • There can be reasons to use AlertDialog ... Rather than rolling your own. – Code Droid May 31 '12 at 05:51
  • thats what i am saying, there can be reasons too, that they let u make custom dialogs, when you do not just want to prompt to user, when you want a user to input somethings and inside that manipulate the input, but you want to display output on same dialog then its no more an alert dialog, its simply a dialog, alert dialogs are to alert the users for something. Like we can make a sign in dialog , it should be in custom dialog not in alert dialog and It provides you everything custom that you take a view same as custom list view. – mayank_droid May 31 '12 at 05:57