10

Please help me i want to get position of imageview on a layout.

enter image description here

5 Answers 5

16
x = imageViewObject.getLeft();
y = imageViewObject.getTop();

Hope, it help you!

3
  • 3
    didn't work for me. Everything returned zero. (Used with Button, not ImageView, but that shouldn't make a difference.) Jan 10, 2015 at 18:54
  • @mathheadinclouds doesn't work for me either. Did you ever get a work around?
    – DIRTY DAVE
    Mar 30, 2020 at 15:55
  • Still no solution? It's 0 for me too Jun 19, 2020 at 12:10
7

You can use this with getLocationOnScreen(int [])

1
  • not work for me. my image with is in a RelativeLaoyout and it relates with a button from the top and aligns with the right Jan 6, 2020 at 22:01
7

At runtime you can get the location of every View object (this includes Layouts too) with

Left Position of View Object getleft()

Top Position of View Object getTop()

Right Position of View Object getRight()

Bottom Position of View Object getBottom()

Even you can get the location with

getLocationOnScreen(int [] )

1

Position meaning x and y and height and width?

This layout is a view, that view has these properties.

2
  • i want to get current location on view means x and y corrdinate, so that i can animate from this position to that position.
    – Programmer
    Feb 3, 2012 at 9:34
  • Look at the methods available in the View class.
    – JoxTraex
    Feb 3, 2012 at 9:35
0

the below code works for me to get the original spot i used the global boolean variable moved to find the value once in the onTouch method v.getX() and ...getY() gets the value of my imageView object I have run this on my apk file on a device and it works perfectly. hope this helps

  joystick.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(moved){
                moved = false;
                xOrigin = v.getX();
                yOrigin = v.getY();
                mode.setText(Float.toString(xOrigin));
                challenge.setText(Float.toString(yOrigin));
            }
            mode.setText(Float.toString(v.getX()));
            challenge.setText(Float.toString(v.getY()));
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                        xCoOrdinate = v.getX() - event.getRawX();

                    yCoOrdinate = v.getY() - event.getRawY();
                    //mode.setText(Float.toString(v.getX()));
                    //challenge.setText(Float.toString(v.getY()));
                    break;
                case MotionEvent.ACTION_MOVE:
                    v.animate().x(event.getRawX() + xCoOrdinate).y(event.getRawY() + yCoOrdinate).setDuration(0).start();
                    if(v.getX() < xOrigin -50){
                        v.animate().x(xOrigin -50).setDuration(0).start();
                    }
                    else if(v.getX() > xOrigin +50) {
                        v.animate().x(xOrigin +50).setDuration(0).start();
                    }
                    if(v.getY() < yOrigin -50){
                        v.animate().y(yOrigin -50).setDuration(0).start();
                    }
                    else if(v.getY() > yOrigin +50) {
                        v.animate().y(yOrigin +50).setDuration(0).start();
                    }
                    break;
                default:
                    v.setX(xOrigin);
                    v.setY(yOrigin);
                    mode.setText(Float.toString(xOrigin));
                    challenge.setText(Float.toString(yOrigin));
                    return false;
            }
            return true;
        }
    });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.