3

How can I detect the double tap event for View in Xamarin.Android? I am failing with TouchListener. There is a SO answer that says double tap is not an Android pattern.

For Native Android (Java), there is an answer, but I want to do that in Xamarin (C#).

Community
  • 1
  • 1
Sagar Panwala
  • 922
  • 1
  • 8
  • 23

2 Answers2

8

It's too easy in c# to do this task. You are new to this world so I'm giving you the exact solution.

First of all you don't have to create a new custom view for this.

Create a class

private class GestureListener : GestureDetector.SimpleOnGestureListener
    {
        public override bool OnDown(MotionEvent e)
        {
            return true;
        }

        public override bool OnDoubleTap(MotionEvent e)
        {
            return true;
        }
    }

Now just write this code.

  GestureDetector _gestureDetector = new GestureDetector (_context, new GestureListener ());

            _gestureDetector.DoubleTap += (object sender, GestureDetector.DoubleTapEventArgs e) => {
                   //apply double tap code here
            };

            //apply touch to your view
            View1.Touch += (object sender, View.TouchEventArgs e) => {
                _gestureDetector.OnTouchEvent (e.Event);
            };

I hope this help you.

Harshadcse
  • 225
  • 1
  • 9
3

The first link in your question already contains the answer. A pity it isn't the accepted answer, though. A C# port of the correct answer would be:

public class DoubleTappableView : View
{
    private readonly GestureDetector _gestureDetector;

    public DoubleTappableView(Context context, IAttributeSet attrs)
        : base(context, attrs)
    {
        _gestureDetector = new GestureDetector(context, new GestureListener());
    }

    public override bool OnTouchEvent(MotionEvent e)
    {
        return _gestureDetector.OnTouchEvent(e);
    }

    private class GestureListener : GestureDetector.SimpleOnGestureListener
    {
        public override bool OnDown(MotionEvent e)
        {
            return true;
        }

        public override bool OnDoubleTap(MotionEvent e)
        {
            //TODO: Add double tap logic here
            return true;
        }
    }
}
Community
  • 1
  • 1
William Barbosa
  • 4,936
  • 2
  • 19
  • 37
  • Thanks. I want to make a DoubleClicked event same as View.Clicked in my activity or fragment. So I can use that in my project. Please guide me. – Sagar Panwala Sep 18 '15 at 17:40
  • That''s actually pretty trivial, simply add an event to this view and raise it inside the OnDoubleTap method. However, there's no need to do so. The above code works perfectly as it is, there's no need to add another layer – William Barbosa Sep 18 '15 at 17:43
  • Can you show me how can I do that? Also I don't want to create custom view. I just want to use Android Views – Sagar Panwala Sep 18 '15 at 17:54
  • There's literally zero complexity in implementing and raising an event (and there are many tutorials on how to do it). Add an event to this class, create its EventArgs and raise it inside OnDoubleTap, providing whichever data you want.Again, I repeat, adding an event here doesn't really help much. – William Barbosa Sep 18 '15 at 17:59