Android Tap, Double Tap, Pan and Pinch Gestures Recognising — part 1

How to use Android Gesture recognizers and handle view touch events correctly

Elvina Sh
ProAndroidDev

--

Photo by Meredith Schomburg from Dribbble

Hi there! In the Navigine team, we’ve been providing indoor and outdoor positioning mobile technologies that enable advanced indoor navigation and proximity solutions for eight years.

Well, it’s time to talk about a very interesting topic. Today we want to immerse you into the world of gestures. We have already written in one of the previous articles about touch events, how to handle them correctly, and how the whole process is arranged. However, for a more professional approach, there is a more aesthetic solution. Of course, you guessed a long time ago that we are talking about gesture recognizers. So, let’s start!

Pan and Tap gestures

Let’s start with the most basic and understandable part for everyone — tap and scroll events. By the way, a little off-topic information, we started using gestures, because we wanted to implement fling in our indoor navigation application. And since then we are inseparable. Enough about that, let’s switch to the main things.

Of course, all this is easier to implement using the usual touch listener. You wrote two lines of code and everything works. However, difficulties will start to appear as soon as you want to complicate gesture handling and add a bunch of other features. This is where the gesture recognizers come to help us.

So let’s try to understand the main overridden methods now. I think there are no problems with understanding taps and presses, but the scroll and the fling may sound a little deceiving and mislead the reader. The word scrolling here means normal movement on the screen. This is a normal action move with one pointer.

As for the fling, it is a slightly more complex mechanism, it is essentially the same move, but at the same time, it is still animated and will move animatedly if there was a strong swipe. It looks very impressive when working with maps and views with large size.

Of course, we have not added all the methods that can be overridden, there are a few more, but they are not so interesting. So if you need it, you can refer to the android documentation or find the source code of GestureDetector.

Pinch gesture

In simpler words, this is the so-called scale. Of course, it can also be implemented using the usual touch event. But it is worth considering that we are writing elegant code and are going to make a more complex system, where we will add some more gesture detectors in the future.

As you can see, unlike the previous listener, this detector has fewer methods, and everything is arranged without incomprehensible functions. You describe the behavior of your view at the beginning of the movement, during and at the end. Meanwhile, I want to say that if you will think a little here, you can also add a fling effect to the scroll. As a result, you will get smooth movements when scrolling, which will continue to fade even after you remove your fingers.

Conclusion

For this whole structure to work, you can create your own listeners and transfer all incoming touch events to them, and they will figure out what behavior your view should have. But of course, all this will work as long as you describe the internals of these methods correctly, otherwise, nothing will work. For starters, you can simply print specific messages inside each method to better understand where which event will go.

I hope this article will be useful to you, if you have any questions, you can ask them in the comments. In the next article, we will tell you more about some interesting gesture detectors.

--

--

Marketing associate at Navigine.com — Integrated software platform for precise indoor and outdoor positioning.