Search code examples
androideventsdetection

Android Event Detection


How does Android detect events? I'm not asking about implementing different handlers for events, but the logic on decide the event types. For example, when the user performs a swipe on the screen, where the Android SDK detects that this is a swipe event instead of a scroll event or other events? Thanks in advance!


Solution

  • Touch events are typically interpreted by a GestureDetector. Typically, touch events are handled by a View by simply passing them to a GestureDetector. The GestureDetector detects when there is some particular gesture (tap, fling, etc.) by analyzing the recent history of touch events. The GestureDetector then notifies one of the registered listeners (often a subclass of SimpleOnGestureListener).

    It's up to the listener to decide what the semantic meaning is of the gesture. For instance, a ScrollView will set an OnGestureListener that reacts to a fling event by scrolling rapidly. A custom view might react to a fling by deleting some object from the view.