Detect a Touch

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 detect and response to a touch event. The user can touch a Button on the screen and then slide it left or right.

Recipe

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

  2. Modify Main.axml to resemble 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="fill_parent"
   android:minWidth="25px"
   android:minHeight="25px">
   <Button
       android:text="My View"
       android:padding="6dip"
       android:textAppearance="?android:attr/textAppearanceLarge"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/myView"
        />
</LinearLayout>
  1. Add two instance variables to Activity1:

_myButton – This will hold a reference to the button on the activity.

_viewX – This will hold the X coordinate of the button inside it’s parent container.

[Activity(Label = "DetectATouch", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity, View.IOnTouchListener
{
   private Button _myButton;
   private float _viewX;
}
  1. Modify OnCreate to obtain a reference to the button. Then call SetOnTouchListener to provide a handler for touch event:
protected override void OnCreate(Bundle bundle)
{
   base.OnCreate(bundle);
   SetContentView(Resource.Layout.Main);
   _myButton = FindViewById<Button>(Resource.Id.myView);
   _myButton.SetOnTouchListener(this);
}
  1. Change MainActivity.cs so that it implements View.IOnTouchListener, and add the OnTouch method that is required by the interface. Add the following code to reposition the Button in response to the touch moving across the screen:
public class Activity1 : Activity, View.IOnTouchListener
{
   public bool OnTouch(View v, MotionEvent e)
   {
       switch (e.Action)
       {
           case MotionEventActions.Down:
               _viewX = e.GetX();
               break;
           case MotionEventActions.Move:
               var left = (int) (e.RawX - _viewX);
               var right = (int) (left + v.Width);
               v.Layout(left, v.Top, right, v.Bottom);
               break;
       }
       return true;
   }
}
  1. Run the application. Touch the button, and move your finger to the right. The Button should move to the right accordingly:

Additional Information

The MotionEvent contains information to describe movements of an object. It has an action code which describes what state change occurred, and a set of values to describe the position of the view and other movement.

Prior to Android 3.0 (API level 11) only one view could receive the touch event.

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.