Skip to content
Advertisement

How to forward click event to Launcher app?

I have a widget placed programatically on the Home screen, and I made it possible for the user to drag it to the place he wishes. I have added an OnTouchListener to the view to get motion events (so that I can remember view position on screen for next time), and it fires wherever I click on the Home screen, even when this is outside of my view.

What I would like to achieve is to forward the MotionEvent.ACTION_UP to the Launcher app so that, if user clicked on an app icon, that app will be launched. Or alternatively, NOT to receive this event when clicked outside of my view. Here’s what I have done:

 LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 inflatedViewWidget = inflater.inflate(R.layout.mtc_appwidget, null);

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                        WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                        0,
                        PixelFormat.TRANSLUCENT);
final WindowManager wm = (WindowManager) context.getSystemService(WINDOW_SERVICE);
wm.addView(inflatedViewWidget, params);

OnTouchListener:

inflatedViewWidget.setClickable(true);
inflatedViewWidget.setOnTouchListener(new View.OnTouchListener() {
       public boolean onTouch(View v, MotionEvent event) {

                        int action = event.getAction();
                        switch (action) {
                            case MotionEvent.ACTION_UP:
                                //how to forward event to Launcher app?
                                break;

Advertisement

Answer

AFAIK, doing a click in another app is not allowed, that would allow overlay apps to make you click on a pay button for example, you would need root or debugging capabilities for that.

So the trick is to play with the window size. Your widget’s window size and location should always match the clickable area. You can do this with the updateViewLayout method on the WindowManager.

A good example can be found here: https://medium.com/@kevalpatel2106/create-chat-heads-like-facebook-messenger-32f7f1a62064

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement