1

I implemented a popup which shows up when I press the hardware menu key. Now I want to dismiss the popup when I press on the menu key again.

I tried this

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_MENU) {
        if (pw.isShowing()) {
            pw.dismiss();
        } else {
            openpopup();
        }
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

here's the open popup method

public void openpop() {
    View view1 = getCurrentFocus();
    showpopup(view1);
}

here's the showpopup method

public void showpopup(View view) {

    pw.setTouchable(true);
    pw.setFocusable(true);
    pw.setTouchInterceptor(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                pw.dismiss();

                return true;
            }

            return false;
        }
    });

    try {
        pw.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
        pw.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        pw.setOutsideTouchable(false);
        pw.setContentView(popupView);
        pw.showAsDropDown(view, 0, 0);
        popUpShowed = true;
    } catch (Exception e) {
        Log.e("SAMPLE", "Exception", e);
    }

}

But the popup is not dismissed when I press menu.

What could be the problem? I need to get this done somehow. Please help me out.

darsh
  • 741
  • 4
  • 10
  • 34
  • 1
    I'm certainly sure that your pop-up is shown and instantly dismissed. Could you please put one Log message into each listener? (Log.i("TEST","POPUP")) and tell me what it does. – Alexis C. Aug 14 '12 at 06:35
  • I did it in each listener. The log appears when I press the menu button and the popup is shown. Once the popup is shown, when I press menu again to dismiss the popup nothing happens and the log is not registering. – darsh Aug 14 '12 at 06:53

3 Answers3

1

Why don't you do this ?

private boolean popUpShowed = false;

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_MENU) {
        if (popUpShowed) {
            pw.dismiss();
            popUpShowed = false;
        } else if(!popUpShowed){
            openpopup();
            popUpShowed = true;
        }
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

And delete the keyListener on your popup.

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
  • Tried, Still not working. I'll edit the question to show the method I use to display the pop up. – darsh Aug 14 '12 at 08:41
1

try this:

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 

if (keyCode == KeyEvent.KEYCODE_MENU) { 
    if (pw.isShowing()) { 
        closePW(); 
    } else { 
        openpopup(); // <- I think you do this already here
    } 
    return true; 
} 

return super.onKeyDown(keyCode, event); 
} 
}

// Put this function in the class which open the popup
// like your function openpopup()
public void closePW(){
   pw.dismiss();
}
JackTools.Net
  • 734
  • 6
  • 13
  • Tried but unfortunately it's not working. I've edited the question, please take a look at the method I use to show the popup. – darsh Aug 14 '12 at 08:47
  • Please, see this : http://android-er.blogspot.ch/2012/03/example-of-using-popupwindow.html – Alexis C. Aug 14 '12 at 09:04
  • Well the only idea i have is to replace pw.isShowing with your booelan popUpShowed like in alexis answer or create a funktion public boolean isPopUpShowed(){return popUpShowed;} – JackTools.Net Aug 14 '12 at 09:06
1

Apparently, pressing Menu Key second time does not trigger.
Found here the solution.
Hope it helps.

EDIT :
Checked and created another approach. This should do it :

boolean open = false;
boolean itemSelected = false;

@Override
public boolean onMenuOpened(int featureId, Menu menu) {

    if(!open || itemSelected) {
        showPopup();
        open = true;
        itemSelected = false;
    }
    return false;
}

@Override
public void onPanelClosed(int featureId, Menu menu) {
    if (open && !itemSelected) {
        closePopup();
        open = false;
    }
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
    itemSelected = true;
    return false;
}

Works for me on Android 4.0.3. Apparently onPrepareOptionsMenu has changed functionality after v3.

Community
  • 1
  • 1
cosmincalistru
  • 1,283
  • 9
  • 20
  • This is what the problem is. Pressing the menu key second time does not trigger. I tried it, but the problem still persist. I doubt whether I understood it wrong. Could you please clarify the method a bit – darsh Aug 14 '12 at 08:49