Challenge - 5 Problems
Custom Layout Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
How does onMeasure affect a custom layout?
In a custom Android ViewGroup, what is the main role of the onMeasure method?
Attempts:
2 left
💡 Hint
Think about how the system knows how big your custom layout should be.
✗ Incorrect
The onMeasure method is where a custom layout calculates how big it and its children want to be. This helps the system allocate space properly.
❓ ui_behavior
intermediate2:00remaining
What does onLayout do in a custom ViewGroup?
In a custom Android ViewGroup, what is the purpose of the onLayout method?
Attempts:
2 left
💡 Hint
Think about where the children should appear inside the parent.
✗ Incorrect
The onLayout method is responsible for placing each child view at the correct position and size inside the parent ViewGroup.
📝 Syntax
advanced2:00remaining
What is the output of this custom layout measurement code?
Given this snippet inside a custom ViewGroup's
onMeasure method, what will be the measured width and height of the ViewGroup?
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val widthSize = MeasureSpec.getSize(widthMeasureSpec)
val heightSize = MeasureSpec.getSize(heightMeasureSpec)
setMeasuredDimension(widthSize / 2, heightSize / 2)
}Attempts:
2 left
💡 Hint
Look at how setMeasuredDimension is called with half the sizes.
✗ Incorrect
The code sets the measured dimensions to half the available width and height, so the ViewGroup will be smaller than the space given.
🔧 Debug
advanced2:00remaining
Why does this custom layout not show its children?
A developer wrote this
onLayout method but the children are not visible:
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
// No calls to child.layout()
}
Why are the children not visible?Attempts:
2 left
💡 Hint
Children need to be told where to appear inside the parent.
✗ Incorrect
Without calling child.layout() for each child, the system doesn't know where to place them, so they don't appear.
🧠 Conceptual
expert3:00remaining
How does padding affect custom layout measurement and positioning?
In a custom ViewGroup, how should padding be handled during
onMeasure and onLayout to correctly size and position children?Attempts:
2 left
💡 Hint
Think about how padding reduces space inside the ViewGroup.
✗ Incorrect
Padding reduces the space available for children, so it must be subtracted from the size during measurement and used as an offset when positioning children.