0

I recently just created a custom view group to test whether or not you really need to include super.onMeasure() or super.onLayout() when extending the view or viewgroup and overriding the onMeasure() and onLayout() method. What I found was that you don't need to include the super.onMeasure() or super.onLayout() and everything still works fine.

So if that is the case what exactly is super.onMeasure() and super.onLayout() use for and why should I even call it to begin with?

Wowzer
  • 1,103
  • 2
  • 12
  • 26

1 Answers1

1

Check the source code & javadoc. That answers the question whether to call super or not in most cases. Do you want to replace the default behavior or inherit it? Replace = don't call. Inherit = call it.

For example some views may only have to do some work in addition to the default, they can call super & then do their bit. That said:

  • onLayout is abstract & therefore empty. It's still legal to call super.onLayout but there is actually no code that can be executed. So don't bother.
  • onMeasure is the default of View and will setMeasuredDimension() based on getSuggestedMinimumWidth/Height() which is most of the time not the behavior you want to inherit in a view group because you'll probably want to adjust your measurements based on your children.

The only thing you shouldn't do is call super.onMeasure() after your code has set measurements. That would destroy your work.

zapl
  • 63,179
  • 10
  • 123
  • 154
  • That's it. If you someday need to read the height/width of a `View` you will need to wait for the measures to be performed and set, before reading them. I don't think they matter if you don't need them, but if you did need them, you would get problems like [this](http://stackoverflow.com/questions/3591784/getwidth-and-getheight-of-view-returns-0) one. – UDKOX May 15 '16 at 02:05