Button button = findViewById(R.id.button); button.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch(event.getAction()) { case MotionEvent.ACTION_DOWN: // User has touched the button break; case MotionEvent.ACTION_MOVE: // User is moving their finger on the button break; case MotionEvent.ACTION_UP: // User has lifted their finger off the button break; } return true; } });
View view = findViewById(R.id.view); view.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // Handle touch event here return true; } });In this example, we have a generic View and we register a touch listener on it using setOnTouchListener(). Inside the listener, we handle the touch event and write code accordingly. Package Library: android.view