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.