'How to make Window Manager window draggable in android

I am using Window Manager to show my Button above all window. But the problem is I want to make it draggable but the touch event is not working as expected when I tried to go down floating window moves up when I tried to move it right it move towards left. Please help me out on this.

Here is my code -

`

@SuppressLint("ClickableViewAccessibility")

 @Override public void onCreate() {
 super.onCreate(); Log.e("Cust", "Started");
 if (mFloatingView == null) { 
mFloatingView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bubble_layout, null); 
mFloatingView.setVisibility(View.GONE); int LAYOUT_FLAG; 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {        
     LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;      
       params = new WindowManager.LayoutParams(                     WindowManager.LayoutParams.WRAP_CONTENT,                     WindowManager.LayoutParams.WRAP_CONTENT,
 LAYOUT_FLAG,                    
 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,               
      PixelFormat.TRANSLUCENT);         } else {           
  LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;       
      params = new WindowManager.LayoutParams(                     WindowManager.LayoutParams.WRAP_CONTENT,                     WindowManager.LayoutParams.WRAP_CONTENT,                  
   LAYOUT_FLAG,                    
 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,                    
 PixelFormat.TRANSLUCENT);         }          //Specify the view position
         params.gravity = Gravity.BOTTOM | Gravity.RIGHT;        //Initially view will be added to bottom-left corner  //Add the view to the window
        mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        mWindowManager.addView(mFloatingView, params);
    }
mFloatingView.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) {

                                }
                                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(mFloatingView, params);
                                return true;
                        }
                        return false;
                    }
                });
}

when i tried to go down floating window moves up when i tried to move it right it move towards left. Please help me out on this.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source