Skip to content
This repository has been archived by the owner. It is now read-only.
Branch: master
Find file History

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.


Type Name Latest commit message Commit time
Failed to load latest commit information.

id title brief sdk
Create a Gesture Listener
This recipe will show how to recognize a fling gesture. A fling gesture is when the user presses on the screen, and while maintaining contact with the screen moves their finger in a given direction.


  1. Create a new Xamarin.Android application named RecognizeGesture.

  2. Edit Main.axml so that its contents match the following:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
       android:text="Small Text"
       android:id="@+id/velocity_text_view" />
  1. Edit MainActivity.cs, and add the following instance variable to the class:
private GestureDetector _gestureDetector;
  1. Edit MainActivity.cs so that it implements Android.Views.GestureDetector.IOnGestureListener and the methods required by that interface. More functionality will be added to the OnFling method further on.
public class Activity1 : Activity, GestureDetector.IOnGestureListener
   public bool OnDown(MotionEvent e)
   public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
   public void OnLongPress(MotionEvent e) {}
   public bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
   public void OnShowPress(MotionEvent e) {}
   public bool OnSingleTapUp(MotionEvent e)
       return false;
  1. Edit the OnFling method so that it will display some values captured when the user "flings" on the screen:
public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
   _textView.Text = String.Format("Fling velocity: {0} x {1}", velocityX, velocityY);
  return true;
  1. Create an instance of GestureDetector for the Activity. Modify OnCreate as shown in the following snippet:
protected override void OnCreate(Bundle bundle)
   _textView = FindViewById<TextView>(Resource.Id.velocity_text_view);
   _textView.Text = "Fling Velocity: ";
   _gestureDetector = new GestureDetector(this);
  1. Override the method OnTouchEvent in the Activity. This will delegate the handling of the gesture to the GestureDetector.IOnGestureListener methods implemented by the class.
public override bool OnTouchEvent(MotionEvent e)
   return false;
  1. Run the application on a device, and fling your finger across the screen. You will see the X and Y velocities change:

Additional Information

Android provides the GestureDetector class to simplify the detection and handling of gestures on a View. GestureDetectore.IOnGestureListener is not the only interface for gesture detection, there is also GestureDetector.IOnDoubleTapListener and GestureDetector.SimpleOnGestureListener.

You can’t perform that action at this time.