0
0
Android Kotlinmobile~20 mins

Custom layouts in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Custom Layout Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
How does onMeasure affect a custom layout?
In a custom Android ViewGroup, what is the main role of the onMeasure method?
AIt calculates and sets the size requirements for the view and its children.
BIt draws the view's content on the screen.
CIt inflates the layout XML file into view objects.
DIt handles user touch events inside the view.
Attempts:
2 left
💡 Hint
Think about how the system knows how big your custom layout should be.
ui_behavior
intermediate
2:00remaining
What does onLayout do in a custom ViewGroup?
In a custom Android ViewGroup, what is the purpose of the onLayout method?
AIt measures the size of the ViewGroup itself.
BIt listens for click events on the ViewGroup.
CIt handles drawing the background color of the ViewGroup.
DIt positions and sizes the child views within the ViewGroup.
Attempts:
2 left
💡 Hint
Think about where the children should appear inside the parent.
📝 Syntax
advanced
2: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)
}
AThe ViewGroup will have zero width and height.
BThe ViewGroup will be the full width and height of the available space.
CThe ViewGroup will be half the width and half the height of the available space.
DThe code will cause a runtime crash.
Attempts:
2 left
💡 Hint
Look at how setMeasuredDimension is called with half the sizes.
🔧 Debug
advanced
2: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?
ABecause the children are invisible by default.
BBecause the children were never positioned with child.layout(), so they have no size or position.
CBecause onLayout is not called automatically by the system.
DBecause the ViewGroup forgot to call super.onLayout().
Attempts:
2 left
💡 Hint
Children need to be told where to appear inside the parent.
🧠 Conceptual
expert
3: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?
ASubtract padding from the available size in onMeasure and offset children by padding in onLayout.
BIgnore padding because it is handled automatically by the system.
CAdd padding to the children's size to make them bigger.
DSet padding to zero before measuring and laying out children.
Attempts:
2 left
💡 Hint
Think about how padding reduces space inside the ViewGroup.