0
0
Android Kotlinmobile~10 mins

Custom drawing (Canvas) in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
AonDraw
BonPaint
Cpaint
Ddraw
Attempts:
3 left
💡 Hint
Common Mistakes
Using draw() instead of onDraw()
Trying to override onPaint() which does not exist
2fill in blank
medium

Complete 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'
AColor.RED
BColor.BLUE
CColor.GREEN
DColor.BLACK
Attempts:
3 left
💡 Hint
Common Mistakes
Using Color.BLUE or other colors by mistake
Not setting the color property
3fill in blank
hard

Fix 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'
AdrawOval
BdrawCircle
CdrawLine
DdrawRect
Attempts:
3 left
💡 Hint
Common Mistakes
Using drawRect or drawLine which draw different shapes
Using drawOval which requires a RectF parameter
4fill in blank
hard

Fill 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'
A50f
B200f
C100f
D150f
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right values
Using wrong float values
5fill in blank
hard

Fill 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'
AFILL
BSTROKE
CGREEN
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using FILL style instead of STROKE
Forgetting the f suffix on strokeWidth
Using wrong color constant