How to dismiss AlertDialog in android
Asked Answered
D

8

52

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?

Dealt answered 13/2, 2013 at 12:13 Comment(6)
Please, use java naming conventions: methods and variable names should start with a lowercase letter.Butene
use dialog.dismiss(); after SetBackground();Arctic
@DaanGeurts - there is no any dismiss() method from AlertDialog.Builder Class.Poree
@Poree right, i've missed that one, your answer should workRime
OptionDialog.setView(null);Samsara
The simplest way I have found to dismiss the AlertDialog is to add Navigator.pop(context); To my buttons' onPressed: property. This particularly useful when keeping things very simple by doing everything in line.Constant
P
152

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();
    }
});
Poree answered 13/2, 2013 at 12:16 Comment(3)
Really nice, the part important is : "NOT CREATE A ( AlertDialog.Builder OptionDialog = AlertDialog.Builder(getActivity()); ) if not create (AlertDialog OptionDialog =new AlertDialog.Builder(this).create(); ) after you can calls .dismiss(); so great, thanks !!!!!Aspergillosis
A note here is that OptionDialog needs to be visible to the onclick listerner in the snippet above. One way to do to this is to declare the AlertDialog as a member variable in the parent activity class.Postremogeniture
should I do optionDialog = null after dismissing it to avoid memory leak?Damascus
A
65

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.

Apyretic answered 27/2, 2014 at 4:43 Comment(2)
d.dismiss(); worked for me, and Yes, this one is more simple and native to AlertDialogStalingrad
I believe this is still the correct answer.Chatav
P
11

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();
        }
    });
Promiscuous answered 13/2, 2013 at 12:19 Comment(3)
I tried this before and not worked for me until I used the code that is in the accepted answer.Dealt
ok i missed thi line AlertDialog.Builder builder = new AlertDialog.Builder(this); now edit the answer...Promiscuous
@Promiscuous what to do when inside a fragment? this will be null.Malfunction
P
6

To dismiss or cancel AlertDialog.Builder

dialog.setNegativeButton("إلغاء", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });

you call dismiss() on the dialog interface

Potion answered 30/10, 2017 at 23:40 Comment(0)
A
2

Here is How I close my alertDialog

lv_three.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                GetTalebeDataUser clickedObj = (GetTalebeDataUser) parent.getItemAtPosition(position);
                alertDialog.setTitle(clickedObj.getAd());
                alertDialog.setMessage("Öğrenci Bilgileri Güncelle?");
                alertDialog.setIcon(R.drawable.ic_info);
                // Setting Positive "Yes" Button
                alertDialog.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // User pressed YES button. Write Logic Here
                    }
                });
                alertDialog.setNegativeButton("İptal", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //alertDialog.
                        alertDialog.setCancelable(true); // HERE

                    }
                });
                alertDialog.show();
                return true;
            }
        });
Astatine answered 6/2, 2017 at 18:43 Comment(0)
L
1

There are two ways of closing an alert dialog.

Option 1:

AlertDialog#create().dismiss();

Option 2:

The DialogInterface#dismiss();

Out of the box, the framework calls DialogInterface#dismiss(); when you define event listeners for the buttons:

  1. AlertDialog#setNegativeButton();
  2. AlertDialog#setPositiveButton();
  3. AlertDialog#setNeutralButton();

for the Alert dialog.

Lapin answered 7/9, 2016 at 5:21 Comment(0)
U
1

With Kotlin:

fun Activity.showDialog() {
    val builder = AlertDialog.Builder(this)
    builder.setMessage(R.string.message_id)
        .setPositiveButton(
            R.string.confirm_action_text_id
        ) { dialogInterface, _ ->
            dialogInterface.dismiss()
            SignUpActivity.start(this)
        }
        .setNegativeButton(R.string.cancel) { _, _ -> }
    builder.create().show()
}
Uptrend answered 31/5, 2022 at 14:6 Comment(0)
S
-2

Just set the view as null that will close the AlertDialog simple.

Samsara answered 16/6, 2017 at 11:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.