Gestures in Android

In Android we have an API to manage the gestures in Android Studio. This class allows the user an interaction with the app by touch screen We will see basic functions that allow gestures like scroll, tap or press the screen:

First to test it We need that our MainActivity extends from onGestureListener and onDoubleTapListener, just for this example, as an alternative we can attach the listener to a view, when this class extends from these classes we need to implements the necessary functions, we will see just basic and more commons.

To implement the listener, we have to create the object GestureDetectorCompat mDetector; to set up the listener to this class:

This example is quite easy, is just a TextView in the middle of the screen as a counter, when we tap once the counter will increase, when you double tap the counter decrease, every time you touch the screen a toast will appear with the location where was touched, if you press the screen the counter will reset to 0 and finally when you scroll down or up the text size will increase or decrease, make sure that all Boolean method return Boolean.

onTouchEvent: This method captures every event coming from touch screen, so it will be called each you touch the screen.

onLongPress: This method will be called when the event coming from the screen is longer than a tap, resetting the TextView to zero.

onScroll: This method is interesting cause have four parameters, first one is the first event, when you start to press the screen, the second one when you drag the finger over the screen and finish, the third and fourth are the distance of the event of X and Y axis, so to change the size of the TextView we will consider the movement of Y axis, so If location of Y in event 1 is bigger than event 2 it means that you scroll down so the size will decrease, the opposite if the location of Y in event 2 is bigger than event 1.

onDoubleTap: This method will be called when the event is a double tap from the screen, decreasing the value of the TextView.

onSingleTapConfirmed: This method will be called when the event coming from the screen is a single tap, increasing the value of the TextView.

You can download the source here Gestures.