Yarden Cohen Yarden Cohen - 2 years ago 104
Android Question

Trying to close alert alert dialog programmatically

I'm trying to do something simple and this was asked many times before in many variants but I couldn't manage to do it in my case..

I'm trying to do this scenario:

enter image description here

I have a popup alert dialog window (of some item) which has a delete button in it, and if you press the delete button it will popup another alert dialog of "are you sure you want to delete" with yes/no buttons.

If you press the yes it will delete the item and then I want both of the dialogs to be closed.

the yes/no dialog is closed by itself because I pressed the button, but the item dialog is not closed and I can't make it closed any how..

Here is my code:

private void popupItem(final int item_id) {

//setup popup window
final AlertDialog.Builder builder = new AlertDialog.Builder(
new ContextThemeWrapper(getActivity(), android.R.style.Holo_Light_ButtonBar_AlertDialog));

LayoutInflater inflater = getActivity().getLayoutInflater();
final View dialoglayout = inflater.inflate(R.layout.template_popup_item_alert, null);


final AlertDialog item_alert = builder.create();

//setup delete button
final ImageButton deleteImageButton = (ImageButton) dialoglayout.findViewById(R.id.template_popup_item_delete);
deleteImageButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
clickDeleteItem(item_id, item_alert);



and here is the delete function:

private void clickDeleteItem(final int item_id, final AlertDialog item_alert) {
// create (another) popup delete for "are you sure? yes/no"
AlertDialog.Builder alert_yesno = new AlertDialog.Builder(getActivity());


alert_yesno.setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

// ... deleting the item(id)..

dialog.dismiss(); // close yes/on dialog (redundant - it is closed by itself anyway)
item_alert.dismiss(); // close the previous popup dialog //~~ NOT WORKING!


alert_yesno.setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.


So as you can see, I'm passing the item dialog pointer into the yes/no dialog so it could close it, but the line just doesn't work..

Everything is running correctly on the debugger, when I reach to the line:


it is pointed correctly to the item dialog, but this command just won't close it..
I tried the
command also but same thing, nothing happens.

What am I missing?
Thanks ahead!

Answer Source

You are calling


instead of


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download