Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.Start your free trial
Matthew Francis6,967 Points
What is the purpose of the return value in onTouchListener?
I've googled it for a bit but I till can't comprehend what the functionality is. I need a super-dumb down explanation of it.
An example would probably clear my confusion.
Seth Kroger56,412 Points
The return value is a boolean that is "True if the event was handled, false otherwise." If you return false, you're saying you aren't finished with the event and should pass it on to the appropriate child view. If you return true, you're saying you are finished with it, and it shouldn't be passed on to any child views.
Daniel Hartin18,106 Points
OnTouchListener is an interface which allows 2 unrelated classes the ability to talk to each other using a pre-determined method structure which in this case is the OnTouchListener. Like any method you don not need to use the values which are passed in, it is just the structure which is agreed upon by the interface and must be adhered to.
The documentation here https://developer.android.com/reference/android/view/View.OnTouchListener.html gives details on the values but I will also try to explain.
The View returned is the view (widget, textBox etc etc) on which the OnTouchListener action was called on you may use this to determine different actions based on which widget was pressed if you re-use your OnTouchListener class and attach it to multiple Views although generally we use anonymous inner classes so I rarely find this useful.
The MotionEvent returned is the full event containing more information about the event. There are multiple events which Android does a great job or grouping together into groups should as onTouch, onLongPress etc etc what is happening here is android is reading the onActionDown and OnActionUp calls and performing logic to determine which kind of method to call. It gets a little confusing and I may be explaining it terribly but the MotionEvent class is more of the nitty gritty information instead of the nicer grouped together calls for onClick, onTouch, onLongPress etc etc if you want to read up more about MotionEvent class the documention is here https://developer.android.com/reference/android/view/MotionEvent.html.
Once you understand these kind of events you can extend View and start making custom widgets etc to import into your XML like you would do a normal TextBox.