Responding to simple touches

One of the primary means for the user to interact with the device is through touch. Often, this is the only way as there is no keyboard or mouse, and other methods might be unavailable or undesirable.

How to do it...

All views provide access to the two most common forms of touch input: single taps and long presses. We respond to these events using either listeners or event handlers:

  1. We respond to taps using the Click event:
    view.Click += (sender, e) => {
      // the user tapped the view
    };
  2. In the same way, we respond to long presses using the LongClick event. To prevent the Click event from also being triggered, we ensure that the Handled property of the EventArgs is set to true:
    view.LongClick += (sender, e) => { // the user ...

Get Xamarin Mobile Development for Android Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.