Gurfuffle
Gurfuffle

Reputation: 784

Setting up onTouch Listeners on specific x,y points on screen

I am trying to set up onTouch Listeners for exact x,y points on the device screen. If the user hovers over these points it would trigger something else in the app.

I'm wondering is there a way to set up an onTouchListener for an x,y position on the screen?

Upvotes: 0

Views: 183

Answers (2)

Sotti
Sotti

Reputation: 14399

@Override
public boolean onTouchEvent(MotionEvent event) {
    int x = (int)event.getX();
    int y = (int)event.getY();

    if(x == desiredPointX && y == desiredPointY)
    {
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
        }
    }

return false;
}

Upvotes: 0

Lubos Horacek
Lubos Horacek

Reputation: 1582

The motion event contains all the information you need.

@Override
public boolean onTouchEvent(MotionEvent event) {
   int x = (int)event.getX();
   int y = (int)event.getY();
   if(x && y in some place){
     _do your work
     return true;
   }
   return false;
 }

Upvotes: 1

Related Questions