Sign in Go Pro

Custom Views and ViewGroups

Custom ViewGroups (Part 2): Supporting Margins with MarginLayoutParams

This lesson is for PRO members.

Upgrade today to get access to all the PRO lessons.

Unlock this lesson

Up next



In this lesson, we will discuss how to enable margins for child views inside of a custom ViewGroup by using MarginLayoutParams.

  • Why margins do not work in a ViewGroup subclass by default
  • What is MarginLayoutParams
  • What four methods of ViewGroup to override to use a subclass of LayoutParams and what does each method to validate and generate LayoutParams for child views
    • checkLayoutParams(LayoutParams p)
    • generateDefaultLayoutParams()
    • generateLayoutParams(LayoutParams p)
    • generateLayoutParams(AttributeSet attrs)
  • How to override these four methods to use MarginLayoutParams in a custom ViewGroup

Lessons in Custom Views and ViewGroups