Search code examples
androidtouch

Touch event not working


I am implementing a game which is having touch events and I have implemented that but not working. Here is my code for touch listener:

public boolean onTouch(View v, MotionEvent event) {

    int action = event.getAction();
    if(action == MotionEvent.ACTION_DOWN)
    {
        if(event.getX()> r.x && event.getX()< (r.x +100) && event.getY()>(r.y-20) && event.getY()<r.y)
        {
            touch = true;
        }
        return true;
    }
    else if(action == MotionEvent.ACTION_MOVE && touch)
    {
        if(event.getX()>0 && event.getX()<220)
        {
            r.x = event.getX();
            r.y = event.getY();
        }
        return true;

    }

    return false;

}

here r is the object of rectangle and x and y are the coordinates of rectangle.


Solution

  • i am writing the touch listener in surfaceview so i have written the listener in activity hence problwm solved