1

I am using this alert dialog:

public  void displayAlert() 
        { 
         new AlertDialog.Builder(getActivity()).setMessage(R.string.invitenotice)   
               .setTitle("Invite Notice")   
               .setCancelable(true)   
               .setNeutralButton(android.R.string.ok,   
                  new DialogInterface.OnClickListener() {   
                  public void onClick(DialogInterface dialog, int whichButton){ 
                     finish();
                  }   
                  })   
               .show();  
        } 

my problem is when I click ok I don't want to use finish(); I just want to dismiss the dialog. Any help is appreciated. Or maybe a more simple way of creating a simple dialog box when I press a button ? Thanks everyone. Also, a problem is I get 4-6 instances of the dialog box. So i need to press ok 4-6 times until it dissapears. Where I put the listener to my button is here :

   findViewById(R.id.mainHelp).setOnTouchListener(new OnTouchListener() { 
            @Override 
            public boolean onTouch(View v, MotionEvent event) { 
                               displayAlert(); 
                   return false; 
}}); 

in the onCreate() method

Fofole
  • 3,398
  • 8
  • 38
  • 59

5 Answers5

4

remove finish() default action is dismiss. so don't put anything in on click().

Yashwanth Kumar
  • 28,931
  • 15
  • 65
  • 69
3

Just set null as the listener, the dialog will dismiss itself.

Jave
  • 31,598
  • 14
  • 77
  • 90
2

You can call dialog.dismiss(); inside you're onClick method.

Nikolay Ivanov
  • 8,897
  • 3
  • 31
  • 34
1

Handle the scenario for Dialog box this way

@Override
public void onClick(DialogInterface dialog, int which)
{
    // TODO Auto-generated method stub
    switch (which)
    {
        case DialogInterface.BUTTON_NEUTRAL:
            break;
    }
}

On click of OK dialog box is dismissed automatically.

Brad Gilbert
  • 33,846
  • 11
  • 78
  • 129
EnthuDeveloper
  • 672
  • 9
  • 25
-1

How about

            AlertDialog alertDialog;
    alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Error!");
    alertDialog.setMessage("Test!");
    alertDialog.show();
    alertDialog.setButton(3,"Yes", new DialogInterface.OnClickListener(){

    @Override
    public void onClick(DialogInterface dialog, int which) {               
            finish();
            }
    });
mkh
  • 1