
Voilà, it works! You are happy and can go back to work. Ok, let’s be radical and try onWindowsFocusChanged(). Maybe onResume() will work? Wait… what?! onResume doesn’t work either?! The next lifecycle step should be when the Activity is already running and you are still unable to finish the UI setup. So you think, maybe the view will be laid out in onStart(). myView had a width of zero and then your code just divided 0 by 2. You add it inside onCreate() right after setting content view, run the application but it doesn’t work as expected.

This sets myView width based on its own width, this means that this code should run only once.

Having this in mind, let’s say we have a View that for some reason needs a manual setup like this: = myView.width / 2 We understand that the onCreate() callback is where the UI should be declared and configured. One of the very basic Android concepts that we first learn is the Activity lifecycle. In the end, I’ll give you a very sweet solution about Android views. In this article I’ll explain why this happens and why many of the most used workarounds should be avoided. We add it to the code without having much idea of what’s going on and that’s it. This is a very common situation, it is one of those problems that we just Google for a solution and find a workaround that seems to work.

I believe that almost every Android dev has at least once tried to get a View’s dimension, but it returned 0, nada.
