0
0
Android Kotlinmobile~20 mins

Custom drawing (Canvas) in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Canvas Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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)
  }
}
AA blue square filling the entire view
BNothing is drawn because onDraw is empty
CA red rectangle at the top-left corner
DA red circle centered in the view with radius 100 pixels
Attempts:
2 left
💡 Hint
Look at the paint color and the drawCircle parameters.
lifecycle
intermediate
1: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?
AInside the constructor or as class properties, before onDraw is called
BInside the onDraw method every time it is called
CInside onMeasure method
DInside onDetachedFromWindow
Attempts:
2 left
💡 Hint
Think about performance and how often onDraw runs.
📝 Syntax
advanced
2: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)
}
ARuntime error because super.onDraw must be called before drawing
BNo error, draws a rectangle and then calls super
CNo visible drawing because Paint() has default color transparent
DCompilation error because super.onDraw must be first line
Attempts:
2 left
💡 Hint
What is the default color and style of a new Paint object?
navigation
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?
Acall setVisibility(View.VISIBLE)
Bcall requestLayout() on the view
Ccall invalidate() on the view
Dcall onDraw() directly
Attempts:
2 left
💡 Hint
Which method tells Android to redraw the view?
🔧 Debug
expert
2: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)
  }
}
AThe strokeWidth is zero, so the circle's outline is invisible
BThe paint color is blue but the style is fill, so no outline is drawn
CThe circle radius is too small to see
DThe onDraw method is missing a call to invalidate() to refresh
Attempts:
2 left
💡 Hint
Check the strokeWidth value and what it means for drawing.