0

Hi so I'm trying to get the location of an imageview in android using

xPosition = view.getLeft();
yPosition = view.getTop();

But it keeps returning 0 no matter where the imageview happens to be. The same also occurs when using

view.getLocationOnScreen(int[] location);

Does anybody have any ideas why this might be?

JVDM
  • 7
  • 3
  • have you bind it an image or not? – pouyan May 29 '16 at 13:49
  • Possible duplicate of [How to get the absolute coordinates of a view](http://stackoverflow.com/questions/2224844/how-to-get-the-absolute-coordinates-of-a-view) – RominaV May 29 '16 at 15:42

1 Answers1

0

You can use those methods after onCreate has completed. One way to do it is this inside the onCreate method is this:

view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            //do your thnig here.....

            if (Build.VERSION.SDK_INT < 16) { 
                view.getViewTreeObserver().removeGlobalOnLayoutListener(this);                                                                                        
            } 
            else { 
                view.getViewTreeObserver().removeOnGlobalLayoutListener(this);  
            } 
        }
    });

The globalLayoutListener will be notified when the layout is built and run the onGlobalLayout method. Be sure to remove the globalLayoutListener after you do what you want to do.

Anonymous
  • 4,470
  • 3
  • 36
  • 67