Ad Code

Responsive Advertisement

Xamarin.Android - Gesture Detector

Introduction

 The gesture detector means finger movement in the touchscreen interface. The gesture types are Tab, Drag, Flick, Swipe, Double Tab, Pinch, Three Finger Pinch, Three Finger Swipe , Touch, Hold, Rotate and Shake. In this article, we are going to learn how to implement these gestures in our application.

Add caption


Let's start the story


Create a Xamarin Android project in Visual Studio 201.Go to the Visual Studio >> Create new project >> select Android App (Xamarin) and click Next. In the Next dialog box, give Project Name, Location Name and Location, then click the Create button. 



After the project creation, open content_main.xml file and add TextView widget in the UI and set width and height as match_parent. The code is given below.



Next, open MainActivity.cs class by double-clicking it. The gesture interfaces are going to implement in this MainActivity.This interface will create following gesture methods:

    • OnDown
    • OnScroll
    • OnSingleTabUp
    • OnLongPress
    • OnFling
    • OnShowPress
Methods with definitions here:
    • OnDown(MotionEvent e) - Down MotionEvent will get triggers, once touch or tab the UI.
    • OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) - Notifies of swipe occurs in the start and end of MotionEvent or threshold of swipe.
    • OnLongPress(MotionEvent e) - This event will trigger, once long touch or tab of UI
    • OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) - while scrolling this event will get trigger.
    • OnShowPress(MotionEvent e) - performed a done MotionEvent and not above performed a moment.
    • OnSingleTabTabUp(MotionEvent e) - On single tab the widget or layout this event.
Based on the Fling start and end velocity, we can set the following swipe directions:
    1. Top to Bottom
    2. Bottom to Top
    3. Right to Left
    4. Left to Right
Velocity has two types 
  1. Swipe threshold - the difference between the initial position and final position of touch in the any four directions, we already seen before.
  2. Velocity threshold - how quickly swiped gesture.
Finally set this interface to that TextView, we already designed in content_main.xml and this page code is give below.



Run your application and get output like below gif


The full source code is here - GitHub
Conclusion
I hope you all, understand the gestures and how to implement our application, Thank for reading.

Reactions

Post a Comment

0 Comments