Answer the question
In order to leave comments, you need to log in
Android studio, getX() returns null due to View elements not being positioned. How to check if screen elements are rendered?
getX() returns zero because the View elements haven't positioned themselves yet. How to fix it?
I looked in Google and found a solution: hang a listener on the button that is executed when the button is drawn (If I understand correctly). Is there a listener that will be executed when all View elements are rendered?
For example, ActionScript3.0 had an ADDED_TO_STAGE listener. Executed when an element is added to the scene.
If there is any other way to get the position of an element, please share.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.mainbutton);
float position = b.getX();
Log.d("myLogs" , String.valueOf( position )); //Returns zero.
}
PS After looking a little more, I found the OnPostCreate() method. It seems that you can get the position of the elements, since it is executed a little later. But it is only available on API 21 and above. And I have Android 4.2 So it does not fit
Answer the question
In order to leave comments, you need to log in
You can use View.getViewTreeObserver().setOnGlobalLayoutListener or something like that.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question