How do I close alert box in onClick method for button that gotten by inflating the layout?


Ask by : DrNutsu November 04, 2012 06:27

I create inflating Layout and I make button that i want to close dialog when click the button.

final AlertDialog.Builder alert = new AlertDialog.Builder(this);
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.subtractalert,null);

the button code

    final Button delete = (Button) textEntryView.findViewById(R.id.deleteButton);
                        delete.setOnClickListener(new OnClickListener() {

                            public void onClick(View v) {

                                // I want to close the dialog at here 

                            }


});

Thanks for all answer, tell me if you want more code :)

View original question

Answer by : EricNovember 04, 2012 06:27

You must first call .show() on the alert variable. You can then use .dismiss() to get rid of the dialog, as long as your statements are within the same method.

final AlertDialog.Builder alert = new AlertDialog.Builder(this);
// ...
final AlertDialog dialog = alert.show();
// ...
delete.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        dialog.dismiss();
    }
});
View original answer