Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do onMeasure in Android returns Size including padding and margin?

Tags:

android

layout

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?

like image 364
Umair A. Avatar asked Sep 22 '11 16:09

Umair A.


People also ask

What is the difference between padding and margin in Android?

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.

What does padding do in XML?

This Attribute is used to specify extra space on Bottom Side inside the view as shown in the below output.

What is the use of padding in Android?

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.

What is onMeasure custom view?

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 ...


1 Answers

The sizes passed in onMeasure includes the padding, but not the margin.

So, while implementing your onMeasure you should keep in mind:

  • The padding.
  • The minimum width and minimum height of our view (use getSuggestedMinimumWidth() and getSuggestedMinimumHeight() to get those values).
  • The widthMeasureSpec and heightMeasureSpec which are the requirements passed to us by the parent.

Ref: Custom View: mastering onMeasure | Lorenzo Quiroli

like image 161
David Miguel Avatar answered Sep 29 '22 19:09

David Miguel