I am working on a Custom Component and would like to know if onMeasure called on children would return width and height of the child including padding and margin of it?
Note that padding goes completely around the content: there is padding on the top, bottom, right and left sides (which can be independent). Margins are the spaces outside the border, between the border and the other elements next to this view. In the image, the margin is the grey area outside the entire object.
This Attribute is used to specify extra space on Bottom Side inside the view as shown in the below output.
Padding can be used to offset the content of the view by a specific number of pixels. For instance, a left padding of 2 will push the view's content by 2 pixels to the right of the left edge.
onMeasure() is your opportunity to tell Android how big you want your custom view to be dependent the layout constraints provided by the parent; it is also your custom view's opportunity to learn what those layout constraints are (in case you want to behave differently in a match_parent situation than a wrap_content ...
The sizes passed in onMeasure
includes the padding, but not the margin.
So, while implementing your onMeasure
you should keep in mind:
getSuggestedMinimumWidth()
and getSuggestedMinimumHeight()
to get those values).widthMeasureSpec
and heightMeasureSpec
which are the requirements passed to us by the parent.Ref: Custom View: mastering onMeasure | Lorenzo Quiroli
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With