I use the following code to drag a view around the screen, and it works. But, when the user first touches moveIcon
, the floatingView
suddenly moves to the center of the screen, even though I want it to remain in its position. How can I fix this? I suspect the problem is in the updatePosition() method.
windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
val layoutInflater = LayoutInflater.from(this)
floatingView = layoutInflater.inflate(R.layout.floating_layout, null)
// Set up the layout parameters for the floating view
val params = WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
@Suppress("DEPRECATION")
WindowManager.LayoutParams.TYPE_PHONE
},
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
)
windowManager!!.addView(floatingView, params)
// Moving the views:
moveIcon = floatingView!!.findViewById(R.id.moveIcon)
moveIcon.setOnTouchListener { view, event ->
when (event.action) {
MotionEvent.ACTION_DOWN -> {
// Save the initial touch coordinates relative to the moveIcon view
initialTouchX = event.rawX - view.x
initialTouchY = event.rawY - view.y
}
MotionEvent.ACTION_MOVE -> {
// Calculate the new position based on the movement and initial touch coordinates
val newX = event.rawX - initialTouchX
val newY = event.rawY - initialTouchY
updatePosition(newX.toInt(), newY.toInt())
}
}
true
}
}
private fun updatePosition(x: Int, y: Int) {
val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
val layoutParams = floatingView!!.layoutParams as WindowManager.LayoutParams
layoutParams.x = x
layoutParams.y = y
windowManager.updateViewLayout(floatingView, layoutParams)
}