Touch Control and Events in Android - GeeksforGeeks

20 Dec, 2021
Almost all Android phones today are touch-controlled. There are very few phones that do not have a touchscreen. In this article, we will discuss how to handle touch events in Android. Understanding how to work around touch controls and touch events when we tap on our screen is an example of this. We’ll go over how Android’s touch events work internally for any view. So, how exactly do input events work, and what happens when we touch our screen when we have a ViewGroup with multiple views inside it? In this case, we’ll look at a LinearLayout with a button on it, such as:
Image #1: The linear layout and the button.
What we will be discussing in this geeks for geeks article is,
What happens when we press our fingers against the screen?How do we stop touch events?How is touch handled?What are the main touch events that we use in Android to handle touch control?What happens when we press our fingers against the screen?
What happens when we press our fingers against the screen?
How do we stop touch events?
How is touch handled?
What are the main touch events that we use in Android to handle touch control?
What happens when we press our fingers against the screen?
So, when we touch the screen, the activity’s view, also known as DecorView in Android, receives the touch event notification first. Now, we don’t usually work with the DecorView touch. As a result, the touch is transferred to the ViewGroup and then to its children in the XML file.
Using dispatchTouchEvent, the ViewGroup in Android transfers the touch event from top to bottom in the ViewGroup hierarchy to its children ().
Image #2: The dispatch event module
To begin, when we perform a touch action, The touch event is then received by the ViewGroup, and it is intercepted within the ViewGroup using onInterceptTouchEvent ().
Geek Tip: If we return true after intercepting, the touch event is not passed to its children; if we return false, the Android eco-system is notified that the ViewGroup wants to dispatch the event to its children, which in our case is a button.
In general, if we return true, it means we handled the event within the ViewGroup itself, and there is no need to dispatch to its children. The view we have now is the last view in our view tree, as indicated by the button. As a result, it will no longer be able to pass on the touch event to its children because it no longer has any. So, in button, our last onInterceptTouchEvent would be called.
We need to check the hierarchy, of all the views that we take into account and then perform our work. Handling the event is all about the priority assigned to everything, the first, and then it moves towards the ViewGroups. Touch events function similarly to event dispatching, but in reverse order from child to parent. Let’s say we dispatch the event from ViewGroup and intercept it there; the return value (true/false) determines whether the view’s touch is handled on the ViewGroup or the children.
Image #3: The dispatchTouchEvent and onTouch
So, in this article, the feedback received from the button returns all true, it means that it has been handled and will not be sent to the LinearLayout.
When we receive a touch event, it is handled by onTouchEvent, which also has a MotionEvent parameter.
Kotlin
fun onTouchEvent(event: MotionEvent)
The event parameter contains a reference to every task performed in relation to the touch. On the screen of the point on the touch, we can have coordinates such as X and Y points. It even has actions in it, for example, if we tap on the screen, MotionEvent.ACTION DOWN is called, and if we lift our finger, MotionEvent.ACTION UP is called. The action is MotionEvent even if you drag your finger across the screen using MorionEvent.ACTION MOVE. So, when we want to tap the button, the flow on the view occurs.
onTouchEvent -> Activity -> dispatchTouchEvent (LinearLayout) -> dispatchTouchEvent (Button) -> onTouchEvent (Button).
When we don’t want to tap the button but still want to handle the click on LinearLayout, the flow is as follows:
Activity -> dispatchTouchEvent (LinearLayout) -> dispatchTouchEvent(Button) -> onTouchEvent(Button) (will return false) -> onTouchEvent(Button) -> onTouchEvent(Button) (will return false) -> onTouchEvent(Button) (LinearLayout).
When working with touch events, we begin by clicking a view and then removing the gesture (in our case, our finger/stylus), after which MotionEvent.ACTION DOWN and MotionEvent.ACTION UP is called.When the initial touch occurs on the ViewGroup and the child is intercepted.MotionEvent.ACTION CANCEL is called on the ViewGroup and the touch event is dispatched to the children.Everything now depends on the return value of onInterceptTouchEvent().The dispatchTouchEvent is dependent on its return value; if it returns true, the dispatcher is canceled; if it returns false, the dispatching of the touch event continues until it is used.And if onTouchEvent() returns true, it means the touch was handled; if it returns false, it means the touch was not handled
When working with touch events, we begin by clicking a view and then removing the gesture (in our case, our finger/stylus), after which MotionEvent.ACTION DOWN and MotionEvent.ACTION UP is called.
When the initial touch occurs on the ViewGroup and the child is intercepted.
MotionEvent.ACTION CANCEL is called on the ViewGroup and the touch event is dispatched to the children.
Everything now depends on the return value of onInterceptTouchEvent().
The dispatchTouchEvent is dependent on its return value; if it returns true, the dispatcher is canceled; if it returns false, the dispatching of the touch event continues until it is used.
And if onTouchEvent() returns true, it means the touch was handled; if it returns false, it means the touch was not handled
Picked
Android
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Resource Raw Folder in Android Studio
Flutter - Custom Bottom Navigation Bar
How to Read Data from SQLite Database in Android?
Flexbox-Layout in Android
How to Post Data to API using Retrofit in Android?
Retrofit with Kotlin Coroutine in Android
Android Listview in Java with Example
Fragment Lifecycle in Android
How to Change the Background Color After Clicking the Button in Android?
How to Get Current Location in Android?