Virtually all phones today running Android are touch-controlled. There are not many phones that aren’t touch-based.
Inside this blog, we are going to discuss how we can handle touch occasions in Android. This is like, understanding how we can work with touch controllers and touch occasions when we tap our screen.
We are going to discuss how signature events in Android operate tirelessly for almost any opinion.
So, how do the input events actually operate and what exactly happens when we touch our screen once we own a ViewGroup having different views within it?
In this case, we are going to talk about a situation where we have a LinearLayout comprising a button such as
What we are to going to Go over in the blog is,
What occurs when we touch the display?
How can we intercept trademark occasions?
How signature is managed?
What are the signature events we wholeheartedly work inside Android for tackling the touch control?
What happens when we touch the screen?
So, when we touch the display the action’s view gets the touch event telling first also called DecorView in Android. We generally do not work with the touch of DecorView. So, the touch gets moved to the ViewGroup and subsequently to its children in the XML file.
However, how can we move the touch event trigger?
How can we intercept trademark events?
First when we perform a touch activity,
Subsequently ViewGroup gets the touch event, then it’s intercepted from the ViewGroup itself with onInterceptTouchEvent().
When on intercepting when we return true then the touch event isn’t passed to its children and if we pass untrue, the Android eco-system gets notified the ViewGroup wants to dispatch the event to its children, in our case it is a button.
Generally speaking, if returning true, it means we’ve handled the occasion in the ViewGroup itself, you don’t have to dispatch to its children.
Now, since the button we have, is the last view in our view tree. Thus, it will not have the ability to pass the signature event to its children anymore since it’s none. So, in button, we would have our last onInterceptTouchEvent being called.
Intercepting of this event can only occur in ViewGroups and not Views.
Today, let us discuss the way the touch is managed in the opinion.
How touch is handled in the view?
When dispatching the touch occasion from top to bottom in the view hierarchy, we will need to see at which location of the tree we need to take care of the touch on the opinion.
When managing the dispatching case, the top place of the ladder takes the lead, however in regards to tackling the signature the kid views using onTouchEvent are always the first, and then it keeps moving towards the ViewGroups.
Touch occasion functions exactly like the dispatching of the events but in the reverse order from child to parent.
Let’s say if we dispatch the event from ViewGroup and intercept case there, it is dependent upon the return value (true/false) that shall the signature of this opinion be managed on the ViewGroup or the children.
In our situation, if the onTouchEvent of Button yields true, then it usually means it has been managed and then, it won’t visit the LinearLayout.
Which are the touch events we wholeheartedly work within Android for handling the signature management?
When we get the signature event, it gets handled by onTouchEvent which additionally has a parameter of type MotionEvent.
All the job done concerning the signature has its reference in case parameter. We can have the coordinates like Y and X points on the display of the point on the signature.
It even has the action in it, like let’s see when we tap on the display then MotionEvent.ACTION_DOWN is known as once we lift the touch MotionEvent.ACTION_UP is known as.
Even dragging a finger over the screen, the activity is MotionEvent.ACTION_MOVE.
Therefore, the stream on the view happens is when we want to tap the button,
And when we don’t want to tap the button but want to Deal with the click on LinearLayout, the flow would be,
Task -> dispatchTouchEvent (LinearLayout) -> dispatchTouchEvent(Button) -> onTouchEvent(Button) (will return false) -> onTouchEvent(LinearLayout).
To summarise everything how to operate around touch control is,
When working on touch events we start by clicking a view and taking away the gesture (in our case our finger/stylus) afterward MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP is called respectively.
When the initial touch happens on the ViewGroup and after intercepting when it moves to the youngster, then MotionEvent.ACTION_CANCEL gets called on the ViewGroup and the signature event dispatches to the kids.
Today, everything is dependent upon onInterceptTouchEvent() and its return value. According to its own return worth the dispatchTouchEvent depends upon that if yields true that the dispatcher is canceled, and if it returns false then the dispatching of the touch event keeps moving on until its used.
And onTouchEvent() if the return value is true, then it means the touch is handled and if it returns false then it means the touch isn’t handled.
Nowwe understand how to control the touch event and how it works. This is very helpful when designing the CustomViews and personal opinion library.