To ensure view has had a chance to update, run your location request after the View's new layout has been calculated by using view.post:
view.post {
val point = IntArray(2)
view.getLocationOnScreen(point)
val (x, y) = point
}
Values SHOULD no longer be 0 but i had bad experiences with huawei devices. Other way to get x and y coordinates is using viewTreeObserver
view.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
view.viewTreeObserver.removeOnGlobalLayoutListener(this)
val point = IntArray(2)
view.getLocationOnScreen(point)
val (x, y) = point
}
});
Possibly you experience some visual glitch with this approach