0

I want to display the popup in the position where the user clicked on the screen, I use the function - showAtLocation (mainView, Gravity.TOP or Gravity.START, posX, posY), everything is fine in the x coordinate, but the Y coordinate of the popup is added higher than that place where the user clicked, and if the main view (mainView) is scrolled, then the popup generally flies to the very bottom and I just can't figure out what's wrong with the Y position. posX and posY is the position where user touch the screen

UPDATED

override fun dispatchTouchEvent(event: MotionEvent): Boolean {    
    posY = event.y.toInt()
    return super.dispatchTouchEvent(event)
}
0

1 Answer 1

0

Thank you for updating the question!

I think showAtLocation() uses total coordinates, which start at the top,left of your screen. But (without seeing the complete context of your code) I think you are taking relative coordinates for the x and y coordinates.

If the viewgroup where you override the function dispatchTouchEvent() has for example a 24dp margin top, the (0;0) coordinate of this view is placed 24dp below the global (0;0) coordinate. Thus when you press at the top,left corner of your viewgroup, event.y.toInt() will return (0;0) and you will return 0 as posY. But your click was 24dp below the total 0 coordinate on your screen. So you will show the popup window 24dp too high.

The same will happen when you scroll down in your mainView. your screen is e.g. 1000px high and you scroll down two times the screenlength. event.y.toInt() could return now 2000px as a relative y coordinate. But the highest screen height is 1000px, so the popup window will be shown at the bottom of your screen, even when you clicked in the middle of your screen.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.