Search code examples
androidandroid-imageviewandroid-screen

Why does location on screen always return 0?


I want to know the position of an image view but its always returns 0 neither the x coordinate or y coordinate. Here is my code

    ImageView credits = (ImageView) v.findViewById(R.id.credits);
    int[] coor = {0,0};
    credits.getLocationOnScreen(coor);
    x = coor[0];
    y = coor[1];

I already use many method such as getTop(), etc. but still its return 0.

Anyone know why? I'm new to android so I really appreciate to any answers.


Solution

  • Are you doing that in the onCreate method ?

    If yes, it's too early to check for getLocationOnScreen() in onCreate()

    Solution here : https://stackoverflow.com/a/13244091/5392383