Question: I have an array an elements. They have similar id:

```
Step2_Visits_0__CountryCode
Step2_Visits_1__CountryCode
Step2_Visits_2__CountryCode
Step2_Visits_3__CountryCode
Step2_Visits_4__CountryCode

```

This array names as "visitCountries".

I use "loop for" and append after each element tag .

For example:

```
for(var i=0; i < 20; i++) {
$(visitCountries[i].id).append("<a id=editCountryItemIcon[" + i + "]" 
    + "style=\"vertical-align: bottom;\"" 
    + "class=\"editLineItemLink\""); 
}

```

But it doesn't work. Please,help!

Answer: 
PopupWindow already has its own dismiss listener, just use it as follows

```
Popup popup = new Popup(getBaseContext());
popup.show(arg1);

```

Change that to

```
Popup popup = new Popup(getBaseContext());

popup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // Do your action
            }
        });

popup.show(arg1);

```
**EDIT**
I hadn't noticed you were extending a PopupWindow, which already has this implemented as shown in @Jayabal's answer. Anyway this is how you would do it if the PopupWindow didn't already have it's own onDismissListener.

Simply create your own OnDismissListener Interface.

```
public interface onDismissListener{

    public void onDismiss();

}

```

Add a reference to a Listener in the PopUp class and a setter.

```
OnDismissListener listener;

public void setOnDismissListener(OnDismissListener listener){
    this.listener = listener;
}

```

Then in your Activity

```
Popup popup = new Popup(getBaseContext());
popup.setOnDismissListener(new OnDismissListener(){

    public void onDismiss(){
        //do what you need to here
    }
});
popup.show(arg1);

```

This pattern should be familiar to you, its used everywhere in Android.