Problema de cierre de ventana emergente

Tengo un menú emergente implementado, que aparece al hacer clic en un botón. Este es mi método onclick.

<code>public void showOverflow(View view) {

    boolean click = true;
    Button action = (Button) findViewById(R.id.btbAction);

    LayoutInflater inflater = (LayoutInflater) main.this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View popupView = inflater.inflate(R.layout.overflow_layout, null);
    final PopupWindow pw = new PopupWindow(popupView,
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    pw.setOutsideTouchable(true);
    if (click) {
        pw.showAsDropDown(action, 0, 0);
        click = false;
    } else {
        pw.dismiss();
        click = true;
    }
}
</code>

La ventana emergente aparece cuando se hace clic en el botón. Ahora, el problema es que la ventana no se cierra cuando toco fuera de la ventana emergente. He intentado establecer esta propiedad en la ventana emergente

<code>pw.setOutsideTouchable(true);
</code>

Las cosas siguen igual. Por favor ayúdame a arreglar esto

Respuestas a la pregunta(2)

Su respuesta a la pregunta