0

I have an image (ImageView) on my screen. The image takes up roughly half of the screen which means that you can click both on the image and on the side of the image.

How to I get the x -and y coordinates on the screen when I click on the image?

I tried using onTouchEvent but it is only called when I click outside of the image, not when I click on the image.

Regards, Mattias

Mattias
  • 599
  • 2
  • 6
  • 11
  • I solved the problem but I can't submit a answer to my own question for another 6 hours because of this site´s policies. I'll post the solution later. – Mattias Jan 18 '12 at 14:10

4 Answers4

5

You can use getLocationOnScreen()

imageView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View view) {
            int[] values = new int[2]; 
            view.getLocationOnScreen(values);
            Log.d("X & Y",values[0]+" "+values[1]);
        }
    });

Also, this answer of mine will be useful as well.

Community
  • 1
  • 1
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • 7
    It seems like I only get the position of the image on the screen. No matter where in the image I click I get (0, 205) back (which is the position of the top left corner of the image on the screen. – Mattias Jan 18 '12 at 13:36
  • 2
    How do you get the coordinates, but outside of the click event, cuz I get 0s. – nikolakoco Jan 26 '13 at 19:05
2

I ended up not setting any OnClickListener on the ImageView at all. Instead I only implement "public void onTouch(MotionEvent event)" and "public void onWindowFocusChanged(boolean hasFocus)" like this:

private int fieldImgXY[] = new int[2];

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    // Use onWindowFocusChanged to get the placement of
    // the image because we have to wait until the image
    // has actually been placed on the screen  before we
    // get the coordinates. That makes it impossible to
    // do in onCreate, that would just give us (0, 0).
    fieldImage.getLocationOnScreen(fieldImgXY);
    Log.i(LOG_TAG, "fieldImage lockation on screen: " + 
            xyString(fieldImgXY[0], fieldImgXY[1]));
}

public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        Log.i(LOG_TAG, "touch event - down");

        int eventX = (int) event.getX();
        int eventY = (int) event.getY();
        Log.i(LOG_TAG, "event (x, y) = " + xyString(eventX, eventY));

        int xOnField = eventX - fieldImgXY[0];
        int yOnField = eventY - fieldImgXY[1];
        Log.i(LOG_TAG, "on field (x, y) = " + xyString(xOnField, yOnField));
    }
    return super.onTouchEvent(event);
}
Mattias
  • 599
  • 2
  • 6
  • 11
  • Can you post xyString() method. – Manikandan Apr 20 '12 at 14:27
  • The app FC stating `x must be <= bitmap.getWidth()' http://stackoverflow.com/questions/21347431/app-is-fc-if-the-user-presses-too-much-to-left-or-right – Si8 Jan 25 '14 at 23:09
  • @Manikandan: I think it should be something like: public String xyString(int x, int y){ return "X:" + String.valueOf(x) + " Y:" + String.valueOf(y); } – EdgarT Mar 18 '14 at 01:20
0
imageview.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    int x = (int) event.getX();
                    int y = (int) event.getY();
                }
                return false;
            }
        });
0

Use the OnClickListener on the ImageView to receive click events, or abstract your on class from ImageView and overwrite the function "public void onTouch(MotionEvent evt)" to get X/Y coordinates from this view.

Andreas
  • 1,617
  • 15
  • 18
  • I ended up not setting any OnClickListener on the ImageView at all. Instead I only implement public void onTouch(MotionEvent event like this: – Mattias Jan 18 '12 at 14:03