add View by using WindowManager, but can back key press
Asked Answered
M

3

4

I've add a View, by using WindowManager.

It shows properly what I wanted to do,

but I have a problem. this is the problem.

  • back key press doesn't affect under android component(like activity)

what I want is my added view can focusable, ( can click the view's inner button ) only when click the view, and outside of the view can process their work. ( for example, if there is a button, can be clicked, and when back key press, top activity was gone )

but if I add a flag - WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, then I can't receive onClick method on my added view's button. but back button work correctly.

otherwise, if i remove the flag -I can receive onClick callback, but now back button doesn't work.

I have a dilema. :(

Thank you.

Macgregor answered 17/10, 2012 at 5:30 Comment(0)
P
10

Have your View override

public boolean dispatchKeyEvent(KeyEvent event) 

to do something when back is pressed.

Psychopathology answered 28/3, 2013 at 4:1 Comment(0)
T
0

Override dispatchKeyEvent 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);
}
Trinette answered 28/3, 2016 at 6:11 Comment(0)
Z
0

You can do like this:

Add a Float window by WindowManager, for example, add a view to screen bottom:

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); 
Zebec answered 25/7, 2017 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.