Create a Gesture Listener

PDF for offline use
Sample Code:
Related SDKs:

Let us know how you feel about this

Translation Quality


0/250

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.

Recipe

  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="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content">
   <TextView
       android:text="Small Text"
       android:textAppearance="?android:attr/textAppearanceMedium"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:id="@+id/velocity_text_view" />
</LinearLayout>
  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)
{
   base.OnCreate(bundle);
   SetContentView(Resource.Layout.Main);
   _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)
{
   _gestureDetector.OnTouchEvent(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.

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.