edi233
edi233

Reputation: 3031

OnClick and onTouch

I have a activity where I have two imagesViews.

This to imagesViews has onClickListener, because I need to know which imageView was clicked. After when I click choosen picture I get result which picture was clicked.

I Know need the same result but I need to know where exacly I click on this image. I need precise coordinates where this imageView was clicked. I know that in onTouch method I have functions like I need.

Can I change onClick method on onTouch? Or in onClick can get precise coordinates?

Upvotes: 0

Views: 2295

Answers (3)

Egor
Egor

Reputation: 40193

There is no need for you to use the onClick event, since you can easily capture the click using the onTouch callback. A click is a sequence of one ACTION_DOWN action, several ACTION_MOVE actions and one ACTION_UP action. These can be acquired using the event.getAction() method. If you get an ACTION_DOWN event and then an ACTION_UP event - it means that the user has just clicked your View. You can also measure time spent between these events to be sure that it was a simple click, not a long one. And, of course, you can use the event.getX() and event.getY() methods to get the exact touch point. Hope this helps.

Upvotes: 4

Th0rndike
Th0rndike

Reputation: 3436

onclick cannot do this. You can get them only from an onTouchListener.

a question with this info

Upvotes: 0

RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

you can use onTouch() method for getting touch coordinates

touchView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        System.out.println("Touch coordinates : " +
            String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
            return true;
    }
});

Upvotes: 3

Related Questions