Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get click position in onClickListener? [closed]

Tags:

java

android

How to get click position with onClickListener? Like follow in onTouchListener.

      int x = (int)event.getX();
      int y = (int)event.getY();
like image 536
NrNazifi Avatar asked Jan 23 '12 14:01

NrNazifi


People also ask

How do you find the click position?

The coordinates of the mouse whenever a click takes place can be found by detecting the click event with an event listener and finding the event's x and y position. A function is created which takes in the canvas element and event as parameters.

Why my setOnClickListener is not working?

You need to put the setOnClickListener in one of the activity callbacks. In your onCreate() method, move the button there and then setOnClickListener() . Save this answer.

What is view OnClickListener()?

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.


1 Answers

Use an onTouchListener with ACTION_DOWN flag

view.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN){
                int x = (int) event.getX();
                int y = (int) event.getY();
            }
            return true;
        }
    });
like image 178
endian Avatar answered Oct 27 '22 21:10

endian