0

The question is simple:

How do I catch clicks on the menu button on a class that extends from PopupWindow?

What I'm doing now is the following:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if (keyCode == KeyEvent.KEYCODE_MENU)
    {
        if (ab != null) {
            ab.showActionOverflowMenu();
        }
    }
    return true;
}

ab.showActionOverflowMenu() does the following:

public boolean showActionOverflowMenu() {
    if (actions.size() >= 4) {
        try {
            if (ag.isVisible()) {
                ag.dismiss();
            } else {
                showActionOverflow();
                return true;
            }
        } catch (Exception e) {
            showActionOverflow();
            Log.d("click", "click");
            return true;
        }
    }
    return false;
}

showActionOverflow() just sets up the popupwindow and attachs it to a button.

tl;dr mode: I want the menu button to dismiss or show a popupwindow.

Charlie-Blake
  • 10,832
  • 13
  • 55
  • 90

1 Answers1

2

You can't catch menu events in PopupWindow and its extendents.

However you can catch the menu event in the Activity which launches the popupwindow and then dispatch it to the popupwindow.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if (keyCode == KeyEvent.KEYCODE_MENU)
    {
        yourPopupWindow.menuClicked();
    }

    return super.onKeyDown(keyCode, event);
}
dor506
  • 5,246
  • 9
  • 44
  • 79
  • Ok, that did work, the problem was my PopupWindow was both Focusable, Touchable and outsideTouchable. Setting it as only Touchable solves the problem. Thanks! – Charlie-Blake May 24 '12 at 13:41
  • Ok, setting the PopupWindow as not Focusable lets me do it but now the popupwindow is unresponsive. What should I do? – Charlie-Blake May 24 '12 at 13:53