Add View using WindowManager, but you can press

I am adding View using WindowManager.

He correctly shows what I wanted to do,

but i have a problem. This is problem.

  • pressing the back key does not affect the android component (for example, activity).

I want my added view to be focused, (you can click the internal view button) only when you click on the view, and outside the view they can handle their work. (for example, if there is a button, you can click, and when you press the back key, the upper activity disappeared)

but if I add the flag - WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, then I can not get the onClick method on my added browse button. but the back button is working correctly.

otherwise, if I remove the flag, I can get the onClick callback, but now the back button does not work.

I have a dilemma .: (

Thanks.

+4
source share
3 answers

Restore your look

public boolean dispatchKeyEvent(KeyEvent event) 

do something when the back button is pressed.

+8
source

Override the KeyEvent dispatcher of your view

  @Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { // handle back press // if (event.getAction() == KeyEvent.ACTION_DOWN) return true; } return super.dispatchKeyEvent(event); } 
0
source

You can do the following:

Add a Float window from WindowManager, for example, add a view to the bottom of the screen:

 WindowManager windowManager = (WindowManager) getActivity().getApplicationContext().getSystemService(Context.WINDOW_SERVICE); WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(); layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT; layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT; layoutParams.type= WindowManager.LayoutParams.TYPE_SYSTEM_ALERT; layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; layoutParams.format = PixelFormat.TRANSLUCENT; layoutParams.gravity = Gravity.LEFT | Gravity.TOP; layoutParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; FrameLayout view = new FrameLayout(getActivity()) { @Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { // do you code return true; } return super.dispatchKeyEvent(event); } }; windowManager.addView(view, layoutParams); 
0
source

Source: https://habr.com/ru/post/1440208/


All Articles