tags:

views:

170

answers:

2

Hi,

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.

Thanks

A: 

Override onTouchEvent(MotionEvent ev)

Then you can do:

ev.getXLocation()

Or something like that. Have a butches.

Tom R
Hi Tom, yes we can use onTouchEvent, but then we need to handle discerning between clicks and long clicks ourselves, don't we? It doesn't make sense that they wouldn't provide you with a way of knowing where the click or long click took place in OnClickListener etc? Thanks
Mark
Romain Guy
Thanks Romain, I'll go with that.
Mark
A: 

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;
        }
    });

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...

Vering