I would like to add a custom view for my application. For this I use WindowsManager:
final WindowManager wm = getWindowManager();
final View view = ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.game_menu, null);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND | WindowManager.LayoutParams.FLAG_FULLSCREEN;
lp.dimAmount = (float) 0.6;
lp.format = PixelFormat.TRANSPARENT;
lp.windowAnimations = android.R.style.Animation_Dialog;
view.setOnKeyListener(new OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
Log.d("12", "12");
if (keyCode == KeyEvent.KEYCODE_BACK)
{
wm.removeView(view); // This I need to hide my menu
}
return false;
}
});
wm.addView(view, lp); // I add menu like in Angry Birds and other games
But I cannot capture device key events for hiding this view.
Why my key listener not invoked in view added by WindowsManager? What must I do to hide my view by device back key pressed?
Views, which added by windows manager not receive onKey event due to android safety politics.