Implementing Menu button click on class extending PopupWindow

739 Views Asked by At

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.

1

There are 1 best solutions below

2
On BEST ANSWER

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);
}