Hello. I inherit the View class, i.e. I am writing my own custom View. To render everything that is necessary in Canvas of my heir to the View class, you need to know the size of my View specified by markup / code.

The getWidth () and getHeight () methods from any place in the class code return 0 ..

Hence the question of how and when to get the real size of the View? And a question to follow: what is the full life cycle of the object heir to View (following the example of Activity - in all the books on Android and articles on the Internet is a diagram with the order of calling callback methods)?

    2 answers 2

    The size of the View becomes known only after View.onMeasure() worked, before that it is unknown, since it depends on a cloud of factors: neighboring views, parents, screen sizes, and so on. other - so measure the size after or during mining View.onMeasure() . Here is what the manual says:

    This is a constructive mistake. If you’re on the track, you’ll be able to get it. The real sizes are calculated during the layout stage, which occurs after construction but before anything is drawn. You can use the GetWidth () and getHeight () methods later, such as the OnDraw () method.

    • Thank you so much. Bull's-eye. Now I know these views more. - zugzug

    While working on View.onMeasure (), an adequate size could not be obtained, but the onSizeChanged () callback method in its parameters contains exactly what you need.