RelativeLayout relativeLayout = findViewById(R.id.relative_layout); relativeLayout.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { // Handle touch event return true; } });
RelativeLayout relativeLayout = findViewById(R.id.relative_layout); relativeLayout.setOnTouchListener(this); @Override public boolean onTouch(View view, MotionEvent motionEvent) { // Handle touch event return true; }This example sets a touch listener on a RelativeLayout object using the activity as the listener and implements the onTouch method in the activity to handle the touch event. Package library: android.widget.