i was just looking up how the views are drawn in android and i was reading the documentation that in provided on the developer site. i understood most of it, except for the following question:
setting context for the question
when a view is drawing itself, it will first call measure on itself and all its children.. wherein it will force constraints on its children based on those forced on it by its parents.
question
now consider that i am drawing a label.. and for some reason the constraint placed on it by its parent only allows it to be drawn in a 60px x 60px space. i have made this label, meaning that it is a custom view. and now i am writing the code for draw method. Over here i am drawing text on the label which has 5 characters and the text size is 20. now for the text to be drawn i would in total require (20*5) 100px, but i can only draw 60px width..
should the measure method not consider the draw method before it returns?
thank you in advance.
The draw method is not considering the needs. OnMeasure is the one that does it. In the onMeasure you return your desired size. This is the place where the component and their parents ‘negotiate’ the size. Of course, you can always end up with insufficient space. But this holds true for all other components – giva a TextView 5px and it will be 5 px. OnDraw is meant just to do that – to draw. All the calculations for the size should have already been made in onMeasure. Newer Android versions have View.MEASURED_STATE_TOO_SMALL (API >= 11) that is returned in onMeasure and indicates that you’re out of space 🙂