Switch branches/tags
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
RecognizeGesture
README.md
RecognizeGesture.sln

README.md

id title brief sdk
5D7E958B-287E-5D68-9BDD-E94E4B2FAFFB
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.

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.