Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnClickListener - x,y location of event?

Tags:

android

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.

like image 942
Mark Avatar asked Dec 27 '09 21:12

Mark


People also ask

How do you find X and Y coordinates on Android?

For Android device screen coordinates, below concept will work. Display mdisp = getWindowManager(). getDefaultDisplay(); Point mdispSize = new Point(); mdisp. getSize(mdispSize); int maxX = mdispSize.

What is the function of 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.

How many ways can you attach OnClickListener to button layout?

Using an OnClickListener There are two ways to do this event handler programmatically : Implementing View. OnClickListener in your Activity or fragment.

What is setOnClickListener new view OnClickListener ()?

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.


2 Answers

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

like image 157
Vering Avatar answered Sep 22 '22 17:09

Vering


Full example

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

Summary

  • Add a class variable to store the coordinates
  • Save the X,Y coordinates using an OnTouchListener
  • Access the X,Y coordinates in the OnClickListener
like image 43
Suragch Avatar answered Sep 20 '22 17:09

Suragch