Draggable floating window in android app : Not focusing on layout elements

Ankesh kumar Jaisansaria :

In my application I am making a floating type window that appears on top of any application or home screen. Like a facebook chat bubble. I have made the floating window drag gable with some buttons for different actions. I have an edittext layout in the window. I am getting some problem in focusing on the floating window. every layout is unfocus and edit text is also not working.

I have attached a screenshoot video of the same.

My code is:-

In service oncreate(){}

    mFloatingWidget = LayoutInflater.from(this).inflate(R.layout.layout_floating_widget, null);
    int layoutType;
    if(Build.VERSION.SDK_INT < 26) {
        layoutType = WindowManager.LayoutParams.TYPE_PHONE;
    } else {
        layoutType = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
    }


    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            layoutType,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.TOP | Gravity.START;
    params.x = 0;   // Initial Position of window
    params.y = 100; // Initial Position of window
    mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    mWindowManager.addView(mFloatingWidget, params);


    mFloatingWidget.findViewById(R.id.root_container).setOnTouchListener(new View.OnTouchListener() {
        private int initialX;
        private int initialY;
        private float initialTouchX;
        private float initialTouchY;
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.d("AD","Action E" + event);
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    Log.d("AD","Action Down");
                    initialX = params.x;
                    initialY = params.y;
                    initialTouchX = event.getRawX();
                    initialTouchY = event.getRawY();
                    return true;
                case MotionEvent.ACTION_UP:
                    Log.d("AD","Action Up");
                    int Xdiff = (int) (event.getRawX() - initialTouchX);
                    int Ydiff = (int) (event.getRawY() - initialTouchY);
                    if (Xdiff < 10 && Ydiff < 10) {
                        if (isViewCollapsed()) {
                            collapsedView.setVisibility(View.GONE);
                            expandedView.setVisibility(View.VISIBLE);
                        }
                    }
                    return true;
                case MotionEvent.ACTION_MOVE:
                    Log.d("AD","Action Move");
                    params.x = initialX + (int) (event.getRawX() - initialTouchX);
                    params.y = initialY + (int) (event.getRawY() - initialTouchY);
                    mWindowManager.updateViewLayout(mFloatingWidget, params);
                    return true;
            }
            return false;
        }
    });


}

SAMPLE

Please let me know what's wrong with the code.

Thanks

Anku Agarwal :

This this happening because the focus is not set to the window manager , instead its on the main screen window. To prevent this set the FLAG_NOT_FOCUS to 0 in params.

Updated Code:-

final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            layoutType,
            0,   // I HAVE CHANGED THIS FROM NOT FOCUS TO ZERO
            PixelFormat.TRANSLUCENT);

params.gravity = Gravity.TOP | Gravity.START;
params.x = 0;   // Initial Position of window
params.y = 100; // Initial Position of window
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mFloatingWidget, params);

Hope this answers your query.

Thanks

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=322797&siteId=1