Challenge - 5 Problems
Canvas Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What will this custom view draw on the screen?
Consider this Kotlin custom view code that overrides
onDraw. What shape and color will appear?Android Kotlin
class MyView(context: Context) : View(context) { private val paint = Paint().apply { color = Color.RED } override fun onDraw(canvas: Canvas) { super.onDraw(canvas) canvas.drawCircle(width / 2f, height / 2f, 100f, paint) } }
Attempts:
2 left
💡 Hint
Look at the paint color and the drawCircle parameters.
✗ Incorrect
The code sets paint color to red and draws a circle at the center with radius 100 pixels.
❓ lifecycle
intermediate1:30remaining
When is the best time to initialize Paint objects in a custom view?
In Android custom views, when should you create and configure Paint objects for drawing?
Attempts:
2 left
💡 Hint
Think about performance and how often onDraw runs.
✗ Incorrect
Paint objects are expensive to create, so they should be initialized once in the constructor or as properties, not repeatedly in onDraw.
📝 Syntax
advanced2:00remaining
What error does this Kotlin code cause in a custom view?
Examine this onDraw override code snippet. What error will it cause?
Android Kotlin
override fun onDraw(canvas: Canvas) {
canvas.drawRect(0f, 0f, width.toFloat(), height.toFloat(), Paint())
super.onDraw(canvas)
}Attempts:
2 left
💡 Hint
What is the default color and style of a new Paint object?
✗ Incorrect
No error. A new Paint() has default opaque black color (alpha=255) and FILL style, so it draws a black rectangle filling the entire view, then calls super.onDraw.
advanced
1:30remaining
How to trigger a redraw of a custom view after data changes?
You updated data that affects your custom view's drawing. Which method should you call to refresh the view?
Attempts:
2 left
💡 Hint
Which method tells Android to redraw the view?
✗ Incorrect
invalidate() schedules a redraw by calling onDraw later. requestLayout() triggers layout pass, not redraw.
🔧 Debug
expert2:30remaining
Why does this custom view not show the expected blue circle?
Given this code, why is the blue circle not visible on screen?
Android Kotlin
class BlueCircleView(context: Context) : View(context) { private val paint = Paint().apply { color = Color.BLUE style = Paint.Style.STROKE strokeWidth = 0f } override fun onDraw(canvas: Canvas) { super.onDraw(canvas) canvas.drawCircle(width / 2f, height / 2f, 50f, paint) } }
Attempts:
2 left
💡 Hint
Check the strokeWidth value and what it means for drawing.
✗ Incorrect
A strokeWidth of 0 means the outline has no thickness, so the circle is not visible even though color is set.