Search code examples
androidoperating-systemscreencoordinate

Effective use of touch/hover co-ordinates on a View


On Android phone, I get the information of co-ordinates that, user touch on his/her phone screen or device. How do i then, use/pass these X-Y coordinates to run a specific function of mine, and use it in my code


Solution

  • reference from the post you can get the co-ordinates if the user touched the screen on your app. After you implement that on touch listener then in the if statement MotionEvent.ACTION_UP put this there

     float myX = event.getX();
     float myY = event.getY();
     // now you can then pass myX,myY in your method or function as parameters
    

    if you happen to be targeting Api 14+ then you can use onhover with the same approach as to the first one from that post, instead of using setOnTouchListener usegetWindow().getDecorView().setOnHoverListener() that's about it.

    A side note is using the firs one is better because onHover can not be trusted to work,actually it doesn't work to me, thats just btw

    hope i answered your question.