How to Check Multi Touch in Android Using Kotlin 2024
Multi touch is a feature that allows users to interact with the screen using more than one finger at a time. For example, you can zoom in or out of a map by pinching the screen with two fingers. In this post, I will show you how to check multi touch in android using kotlin, a modern and concise programming language for android development.
What is MotionEvent?
To check multi touch, we need to use the MotionEvent
class, which represents a touch event on the screen. A MotionEvent
object contains information such as the coordinates, pressure, size, and action of each touch point. A touch point is a finger or a stylus that touches the screen.
A MotionEvent
can have multiple touch points, depending on how many fingers are touching the screen. Each touch point has an index and an ID. The index is the position of the touch point in the MotionEvent
object, starting from 0. The ID is a unique identifier for the touch point, which does not change as long as the finger stays on the screen.
How to Override onTouchEvent Method?
To handle touch events, we need to override the onTouchEvent
method of the View
class, which is the base class for all UI components in android. The onTouchEvent
method takes a MotionEvent
object as a parameter and returns a boolean value indicating whether the event was consumed or not.
The onTouchEvent
method is called whenever a touch event occurs on the view. The MotionEvent
object contains the action and the touch points of the event. The action is a constant that indicates what kind of touch event it is, such as ACTION_DOWN
, ACTION_MOVE
, ACTION_UP
, etc. The touch points are accessed by using the getPointerCount
, getPointerId
, getX
, getY
, etc. methods of the MotionEvent
object.
How to Check Multi Touch?
To check multi touch, we need to use the getPointerCount
method of the MotionEvent
object, which returns the number of touch points in the event. If the pointer count is more than one, it means that there is multi touch. We can also use the getPointerId
method to get the ID of each touch point and compare them to see if they are different.
Here is an example of how to check multi touch in the onTouchEvent
method using kotlin:
private var count = 0 // a variable to store the pointer count
override fun onTouchEvent(event: MotionEvent): Boolean {
val pointersCount = event.pointerCount // get the pointer count
count = when (event.actionMasked) { // check the action
MotionEvent.ACTION_UP -> 0 // reset the count to 0 when the last finger is lifted
else -> pointersCount // otherwise, assign the pointer count to the count variable
}
return true // consume the event
}
In this example, we use a when
expression to check the action of the event. If the action is ACTION_UP
, it means that the last finger has left the screen, so we reset the count to 0. Otherwise, we assign the pointer count to the count variable. This way, we can keep track of how many fingers are touching the screen at any time.
Conclusion
In this post, we learned how to check multi touch in android using kotlin. We used the MotionEvent
class to get the information about the touch event, such as the action and the touch points. We overrode the onTouchEvent
method of the View
class to handle the touch event and get the pointer count. We used a variable to store the pointer count and used it to perform different actions based on the number of fingers.
Thank you for reading! 😊