Handling touches

To get started with screen interaction, add the OnTouchEvent function to the LiveDrawingView class as follows:

override fun onTouchEvent(
   motionEvent: MotionEvent): Boolean {
  
   return true
}

This is an overridden function, and it is called by Android every time the user interacts with the screen. Look at the one and only parameter of onTouchEvent.

It turns out that motionEvent has a whole bunch of data tucked away inside it, and this data contains the details of the touch that just occurred. The operating system sent it to us because it knows we will probably need some of it.

Note that I said some of it. The MotionEvent class is quite extensive; it contains within it dozens of functions and properties.

For now, all we need to know is ...

Get Android Programming with Kotlin for Beginners now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.