4

I have a dynamically created Overlay Window that has an OnTouchListener on a certain layout in the Window. When I detect a a touch with the OnTouchListener, I wish to remove this Overlay Window. This overlay is dynamically created and not part of an activity so does not follow the the Activity lifecycle, hence I can't call finish(). How do I remove the view then? The code snippet below is where I create the View, and where my OnTouchListener is. How should I remove the overlay?

    params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            flags,
            PixelFormat.TRANSLUCENT);
    LinearLayout l = new LinearLayout(service);
    RelativeLayout mainView = new RelativeLayout(service);
    RelativeLayout.LayoutParams param_one = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    param_one.leftMargin=0;
    param_one.topMargin=0;
    l.setTag("first_tag");
    l.setOrientation(LinearLayout.VERTICAL);
    l.setBackgroundColor(0x00ffffff);

    EditText second_test = new EditText(service);
    second_test.setBackgroundColor(Color.parseColor("#0b0b0b")); //TODO: not perfect color
    second_test.setTextColor(Color.LTGRAY);
    second_test.setWidth(440);
    second_test.setTag("second_tag");
    second_test.setTextSize(20);
    LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    second_test.setLayoutParams(llp);
    l.addView(second_test);
    mainView.addView(l, param_one);

    LinearLayout bottomLayout = new LinearLayout(service);
    RelativeLayout.LayoutParams bottomparam = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    bottomparam.width=700;
    bottomparam.height=120;
    bottomparam.leftMargin=10;
    bottomparam.topMargin=260;
    bottomLayout.setOnTouchListener(new OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.d(TAG, "onTouch");
            // Remove the overlay here
            return true;
        }
    });
    mainView.addView(bottomLayout, bottomparam);

I tried to call WindowManager.removeView(thisView) but system services such as WindowManager are not available to Activities before onCreate().

user1118764
  • 9,255
  • 18
  • 61
  • 113

0 Answers0