Gesture Detector in Android Development

Tim Buchalka
A free video tutorial from Tim Buchalka
Java Python Android and C# Expert Developer - 807K+ students
4.5 instructor rating • 10 courses • 812,028 students

Learn more from the full course

Android Java Masterclass - Become an App Developer

Improve your career options by learning Android app Development. Master Android Studio and build your first app today

60:07:47 of on-demand video • Updated July 2020

  • You’re taught step by step HOW to build Android 7 apps for Google’s Nougat platform that will work on older Android versions.
  • With each comprehensive step, the WHY you’re doing it is explained.
  • You’ll have EXPERT LEVEL knowledge of the Java programming language and know exactly how each process of Android Nougat development works.
  • The course is continually UPDATED, so you’ll learn the very latest as Android Nougat evolves.
  • NEW CONTENT is always being added, and you're covered with full lifetime access to the course.
  • SUPPORT is mind blowing – questions you have are answered that day.
  • The EXPERTISE to be an Android Nougat app developer as taught by the best.
  • The skills you’ll learn are in HIGH DEMAND. You’ve learned to program like an expert. Go get that job!
English so our flickr browser apps coming along nicely and in the process we've covered a lot of techniques that you find useful when you go to develop your own apps time now to extend the flickr browser and if we want to display the full photos full screen rather than just the thumbnails in the list we're going to need some way to detect that an item in the recycle view was tapped and we also need to know which item was tapped now one consequence of Google's simplification of the recyclerview is that it no longer has an easy way to set an onclicklistener now there are a number of methods for detecting when an item in the list is tapped and a common method is to implement the recyclerview.onitem touchlistener interface in a class we're going to do something similar but will extend the recycleview.simple onitemtouchlistener extend that class instead google have created this class so that we don't have to implement methods of the interface that we won't use and also to future-proof our apps against changes to the recycler view and we have a look at the google documentation on just going to open a new tab here pasting the link which is in the resources section of this video so if you decide to implement the interface this is a good starting point this page contains a link to the simple on item touch listener this one here and obviously take you to the page to get details about that as well and that links also in the resources section they actually mentioned at the start that extending the class is useful if you don't want to override all the methods of the interface and you can see here I actually talk about guaranteeing to always provide a default implementation in this class if things do change so that your existing code won't break so for that reason alone it's worth considering extending the simple on item touch listener class rather than implementing the on item touch listener interface and now that's what i'm going to do here if you want to use the interface the approach is very similar to what we're going to do in fact the class constructor and methods we define can be used unchanged you just have to override another couple of methods even if you don't use them but before we get back into the code there's another way to do this is actually pretty neat so I'm going to to open up an article on a website and again the links in the resources section and very useful if you want to actually investigate another approach so the first bit of code on that page or in this page shows what adding the listener looks like and once you've created the class it's actually quite simple to add the listener to the recycler view so you may want to look into that further if you're using a lot of the recycler views in the same app but for our app we're going to extend a simple class let's switch back over to android studio and get started we need to create a new class so I'm going to right-click on the package as I have always done new class we're going to call this one a recycler item click listener and of course we're calling it that because that's what it is and click on package private to make it package private click ok close down the project pane we're going to extends recycler.... ...and we're going to do a log t here again there's another example their of the live template truncating the tag and I mentioned that briefly in a previous video but the Android logging library only allows the tag to be 23 characters and in this case our class name is longer than that so actually truncate it if it was more than 23 characters we actually get an error when we run the program and that's another advantage to use the shortcut rather that typing it in yourself now all the approaches to implement a recycler view listener use a callback mechanism which is the usual way of notifying and notifying an activity that something's been clicked so we need to define an interface that we can use to call back on going to call ours on recycler click listener so type...and the two methods were going to define.... .... ....after that we also need then a private.... .... ....and we are getting errors because they haven't been initialized which we're going to be doing shortly in the constructor so we have seen the m listener field used a few times to store the object that will be calling back but what's this gesture detector compat all about well so far we've responded to simple taps on widgets such as buttons or the list view but android actually provides a lot of ways that a user can interact with apps including things like long tapping swiping and drawing patterns and these are all handled by a gesture detector now I've actually used the gesture detector compact class so that our apps compatible with earlier android versions you can find the reference documentation on the gesture detector class by googling the class name and google also provides a training document which will also come up in the search results let's just quickly look at that so searched for gesture detector you can see the search results here so if you want to learn more when you come to write apps that use more complex gestures then you'll find what you need to get started in these links now I know I have been pushing the documentation a lot but the reality is that both JP and myself believe this very strongly and given that we've got you know 30 years experience software development each I think we're in a position to be telling you that this is definitely the way to go because the reality is in our opinion you cannot be a successful developer without taking full advantage of all the resources available on the internet now things are changing so fast and it's just so much that android can do that you just can't learn all of it the trick is to learn what you need when you needed building what you know about how the android classes work and also how Android itself handles things we started writing this course which is based of course on Android nougat at around the same time that samsung started deploying marshmallow on their devices so most devices out there are still a lollipop or earlier and marshmallow was just being rolled out by phone manufacturers when the first previews of the nougat become available working in an environment that's changing so rapidly we would be completely lost without access to all the information that Google and others make available to do make sure you take the time to look at this documentation use the fantastic search capabilities of google to find the information you need and ultimately will make you a better program alright lecture over let's go back to our class so we need a constructor so i'm going to create a constructor now command n again alt insert on a pc and the only field that we want from the constructor is the listener we do need a couple of parameters but only one from the listener so making sure that just the listener is selected and not the gesture detector click on ok we are going to actually add some of these so we're going to before those we're going to add a context context as a parameter and we're going to a final recycler view recycler view so their will be the three parameters will be passed to the constructor for this recycler item view listener and then we just setting the value at the moment in our constructor now we need a context for the gesture detector to work and we also need a reference to the recyclerview that we're detecting the taps on and that's why they're actually added as parameters in the constructor now inside the constructor we're going to initialize the gesture detector to null and what I might do just to make that easy i'm going to actually change the name just call it the m gesture detector even though the type is still just gesture detector compact because it actually makes more sense for us to refer to it as that a little bit easier rather than the word compat which is not really adding much value so.... it won't stay set to null but I want to be able to create an instance of the class and use logging to show what's happening ok so this recycler click item click listener class extends the simple on item touched listener so we can override the on interrupt touch event method to intercept all touch events that might happen on the recycler view what that means is the method will be called whenever any sort of touch happens whether it's a tap Double Tap a swipe or whatever i'm going to get the code generator to override that so we're looking for the on intercept touch events i'm just going to select that one and we'll put some logging in there just at the fact it starts and we'll leave it on the return for now all right we now need to connect this listener to the recycler view in main activities oncreate method swing back to main activity and in the on create first will make mainactivity implement the on recycler click listener interface so we can just pass the activity rather than using an anonymous classes now we did that before when we used main activity as the listener for the buttons in the YouTube app let's go ahead and do that at the moment we've got implements get flickr Json data so lets add a new line there.... .... obviously we're getting an error now because we haven't implemented those methods let's go ahead now and implement those two methods and a little down to the bottom and we'll just do an implement so control I I want to implement both on item click and on item long click click OK and both get added as you can see and for now let's just add some logging and also a toast message .....and we'll be passing main activity.... .... ... ..... .... all that is left now is to create an instance of our recycler item click listener class and added as a touch listener to the recycler view so to do that we're going back to oncreate and just below the set layout manager I'm going to do.... .... we can pass this as both the context because it's an activity and the listener because we've implemented the required interface that's why are able to do that alright I'm going to finish the video here so the next video will give this application a run and see whether the gesture detector works see you in the next video