Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a custom View class that overrides the onDraw method.
Android Kotlin
class MyView(context: Context) : View(context) { override fun [1](canvas: Canvas) { super.onDraw(canvas) } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using draw() instead of onDraw()
Trying to override onPaint() which does not exist
✗ Incorrect
The onDraw method is the correct method to override for custom drawing in a View.
2fill in blank
mediumComplete the code to create a Paint object with red color.
Android Kotlin
val paint = Paint().apply {
color = [1]
style = Paint.Style.FILL
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Color.BLUE or other colors by mistake
Not setting the color property
✗ Incorrect
To draw in red, set the paint color to Color.RED.
3fill in blank
hardFix the error in the code to draw a circle at position (100, 100) with radius 50.
Android Kotlin
canvas.[1](100f, 100f, 50f, paint)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using drawRect or drawLine which draw different shapes
Using drawOval which requires a RectF parameter
✗ Incorrect
The drawCircle method draws a circle with given center coordinates and radius.
4fill in blank
hardFill both blanks to draw a blue rectangle from (50, 50) to (200, 150).
Android Kotlin
val rect = RectF([1], 50f, [2], 150f) canvas.drawRect(rect, paint)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right values
Using wrong float values
✗ Incorrect
The rectangle's left is 50f and right is 200f to match the coordinates.
5fill in blank
hardFill all three blanks to create a Paint object with stroke style, stroke width 10f, and green color.
Android Kotlin
val paint = Paint().apply {
style = Paint.Style.[1]
strokeWidth = [2]f
color = Color.[3]
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using FILL style instead of STROKE
Forgetting the f suffix on strokeWidth
Using wrong color constant
✗ Incorrect
Stroke style is STROKE, width is 10f, and color is green.