Home » Android » Get position of imageview on layout android

Get position of imageview on layout android

Posted by: admin May 14, 2020 Leave a comment

Questions:

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

enter image description here

How to&Answers:

You can use this with getLocationOnScreen(int [])

Answer:

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

Hope, it help you!

Answer:

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 [] )

Answer:

Position meaning x and y and height and width?

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

Answer:

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