public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); switch(action) { case MotionEvent.ACTION_DOWN: // user presses down on the screen break; case MotionEvent.ACTION_UP: // user releases their finger from the screen break; } return true; }
public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); switch(action) { case MotionEvent.ACTION_DOWN: x1 = event.getX(); y1 = event.getY(); break; case MotionEvent.ACTION_UP: x2 = event.getX(); y2 = event.getY(); // Check for left to right movement if (x1 < x2) { // do something } // Check for right to left movement if (x1 > x2) { // do something } // Check for top to bottom movement if (y1 < y2) { // do something } // Check for bottom to top movement if (y1 > y2) { // do something } break; } return true; }
public boolean onTouchEvent(MotionEvent event) { int action = event.getActionMasked(); switch(action) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_POINTER_DOWN: // One or more fingers pressed down break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: // One or more fingers released break; case MotionEvent.ACTION_MOVE: // One or more fingers moved int pointerCount = event.getPointerCount(); for (int i = 0; i < pointerCount; i++) { int pointerId = event.getPointerId(i); float x = event.getX(pointerId); float y = event.getY(pointerId); // do something with each finger's position } break; } return true; }In conclusion, the MotionEvent class belongs to the android.view package library and it is used to handle input events in Android apps, such as touch, gesture or motion events.