Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dismiss AlertDialog in android

I created AlertDialog that contains 4 buttons

OptionDialog = new AlertDialog.Builder(this);
        OptionDialog.setTitle("Options");
        LayoutInflater li = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = li.inflate(R.layout.options, null, false);
        background = (Button) v.findViewById(R.id.bkgSpinnerLabel);
        SoundVib = (Button) v.findViewById(R.id.SoundVibSpinnerLabel);

        OptionDialog.setView(v);
        OptionDialog.setCancelable(true);
        OptionDialog.setNeutralButton("Ok",
                new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface arg0, int arg1) {
                    }
                });
        background.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                SetBackground();
             // here I want to dismiss it after SetBackground() method 
            }
        });


        SoundVib.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent soundVibIntent = new Intent(SebhaActivity.this, EditPreferences.class);
                startActivity(soundVibIntent);
            }
        });

        OptionDialog.show();

I want to dismiss it after SetBackground() method, how can I do this? thanks in advance.

like image 991
Emy Alsabbagh Avatar asked Feb 13 '13 12:02

Emy Alsabbagh


People also ask

How do I turn off AlertDialog on Android?

AlertDialog generally consists of the main title, the message, and two buttons, technically termed as a positive button and a negative button. Both positive and negative buttons can be programmed to perform various actions. By default, the negative button lets close the AlertDialog without any additional lines of code.

How do I dismiss AlertDialog in Kotlin?

Android AlertDialog class is used to display a dialog box to alert the user with positive and negative buttons. Positive button is used to continue with the action specified. Negative button is used to dismiss the alerted action.

How do I dismiss all dialogs in Android?

I use onCreateDialog(int id) to create each dialog and I use showDialog(int id) and dismissDialog(int id) method show and dismiss each dialog respectively.


3 Answers

Actually there is no any cancel() or dismiss() method from AlertDialog.Builder Class.

So Instead of AlertDialog.Builder optionDialog use AlertDialog instance.

Like,

AlertDialog optionDialog = new AlertDialog.Builder(this).create(); 

Now, Just call optionDialog.dismiss();

background.setOnClickListener(new OnClickListener() {     public void onClick(View v) {         SetBackground();         // here I want to dismiss it after SetBackground() method          optionDialog.dismiss();     } }); 
like image 164
user370305 Avatar answered Sep 25 '22 12:09

user370305


I think there's a simpler solution: Just use the DialogInterface argument that is passed to the onClick method.

AlertDialog.Builder db = new AlertDialog.Builder(context);         db.setNegativeButton("cancel", new DialogInterface.OnClickListener(){             @Override             public void onClick(DialogInterface d, int arg1) {                 db.cancel();                 //here db.cancel will dismiss the builder              };           }); 

See, for example, http://www.mkyong.com/android/android-alert-dialog-example.

like image 45
John R.B. Palmer Avatar answered Sep 22 '22 12:09

John R.B. Palmer


Try this:

   AlertDialog.Builder builder = new AlertDialog.Builder(this);
   AlertDialog OptionDialog = builder.create();
  background.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            SetBackground();
       OptionDialog .dismiss();
        }
    });
like image 42
Android_coder Avatar answered Sep 22 '22 12:09

Android_coder