I have a custom view derived from View. I'd like to be notified when the view is clicked, and the x,y location of where the click happened. Same for long-clicks.
Looks like to do this, I need to override onTouchEvent()
. Is there no way to get the x,y location of the event from an OnClickListener
instead, though?
If not, what's a good way of telling if a motion event is a 'real' click vs a long-click etc? The onTouchEvent
generates many events in rapid succession etc.
For Android device screen coordinates, below concept will work. Display mdisp = getWindowManager(). getDefaultDisplay(); Point mdispSize = new Point(); mdisp. getSize(mdispSize); int maxX = mdispSize.
In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.
Using an OnClickListener There are two ways to do this event handler programmatically : Implementing View. OnClickListener in your Activity or fragment.
You set an OnClickListener instance (e.g. myListener named object)as the listener to a view via setOnclickListener() . When a click event is fired, that myListener gets notified and it's onClick(View view) method is called. Thats where we do our own task. Hope this helps you.
Thank you. That was exactly what I was looking for. My code now is:
imageView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN){ textView.setText("Touch coordinates : " + String.valueOf(event.getX()) + "x" + String.valueOf(event.getY())); } return true; } });
which does precisely what Mark asked for...
The other answers are missing some details. Here is a full example.
public class MainActivity extends AppCompatActivity { // class member variable to save the X,Y coordinates private float[] lastTouchDownXY = new float[2]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // add both a touch listener and a click listener View myView = findViewById(R.id.my_view); myView.setOnTouchListener(touchListener); myView.setOnClickListener(clickListener); } // the purpose of the touch listener is just to store the touch X,Y coordinates View.OnTouchListener touchListener = new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // save the X,Y coordinates if (event.getActionMasked() == MotionEvent.ACTION_DOWN) { lastTouchDownXY[0] = event.getX(); lastTouchDownXY[1] = event.getY(); } // let the touch event pass on to whoever needs it return false; } }; View.OnClickListener clickListener = new View.OnClickListener() { @Override public void onClick(View v) { // retrieve the stored coordinates float x = lastTouchDownXY[0]; float y = lastTouchDownXY[1]; // use the coordinates for whatever Log.i("TAG", "onLongClick: x = " + x + ", y = " + y); } }; }
OnTouchListener
OnClickListener
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With