May 2022
Intermediate to advanced
688 pages
20h 12m
English
One way to listen for touch events is to set a touch event listener using the following View function:
fun setOnTouchListener(l: View.OnTouchListener)
This function works the same way as setOnClickListener(View.OnClickListener). You provide an implementation of View.OnTouchListener, and your listener will be called every time a touch event happens.
However, because you are subclassing View, you can take a shortcut and override this View function instead:
override fun onTouchEvent(event: MotionEvent): Boolean
This function receives an instance of MotionEvent, a class that describes the touch event, including its location and its action. The action describes the stage of the event:
Read now
Unlock full access