0

This is my code :

  windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
  LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
  menubuttonClosed = li.inflate(R.layout.menu_button, null);
  menubutton = (ImageButton) menubuttonClosed.findViewById(R.id.menubutton);
  params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.FILL_PARENT,
            WindowManager.LayoutParams.FILL_PARENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);
  params.x = 0;
  params.y = 0;

  menubutton.setOnTouchListener(new View.OnTouchListener() {
  @Override
  public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                return true;
            case MotionEvent.ACTION_UP:
                Log.i("midoka", "click");
                return true;
            case MotionEvent.ACTION_MOVE:
                return true;
    }
    return false;
  }
  });
  windowManager.addView(menubuttonClosed, params);   

I wanted to add a layout with a button to the windowmanager, the button should respond to events (click, touch..), but the layout must keep sending touch events to the window behind, is there a way to do that ?

Mehdi
  • 974
  • 1
  • 10
  • 24

1 Answers1

0

You should use a floating view. For instance, to display a square view with 3dp margin top and 40dp margin right (wm is the WindowManager instance):

private void addMyView(int height) {

            int marginTop = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, getResources().getDisplayMetrics());
            int marginRight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics());
            try {
                WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                        width, height,marginRight,marginTop,
                        WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                        PixelFormat.TRANSLUCENT);
                params.gravity = Gravity.RIGHT | Gravity.TOP;

                wm.addView(view, params);
            }catch(Exception e){

            }

}

EDIT: To remove the view you can call:

wm.removeViewImmediate(view);

EDIT: Dont forget the permission:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

Hope it helps.

Junior Buckeridge
  • 2,075
  • 2
  • 21
  • 27