Created
August 28, 2022 20:28
-
-
Save s3va/c684c53863ba73024c96e4c50a3038dd to your computer and use it in GitHub Desktop.
When can I View.getX() from View?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
view.getViewTreeObserver().addOnGlobalLayoutListener( | |
new ViewTreeObserver.OnGlobalLayoutListener() { | |
@Override | |
public void onGlobalLayout() { | |
// Layout has happened here. | |
// Don't forget to remove your listener when you are done with it. | |
view.getViewTreeObserver().removeOnGlobalLayoutListener(this); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/a/32400275/11798617
ViewGroups such as RelativeLayout do not layout their children immediately, and thus your View does not yet know where it will lie on screen. You need to wait for the RelativeLayout to complete a layout pass before that can happen.
You can listen for global layout events like so: