Listeners are pretty much callback functions implemented through Java Interfaces:
Interfaces are commonly used in the Java language for callbacks. Java does not allow the passing of methods (procedures) as arguments. Therefore, the practice is to define an interface and use it as the argument and use the method signature knowing that the signature will be later implemented.I will explain it with a simple example. Lets say we create our own custom view for our new innovative user interface that is a bit more complex so that a normal Android Button just isn't satisfactory enough for our needs. Lets say we want to have something like a color picker in it:
In this case we would have our custom View ColorPicker, which senses for touch events in our color field and then calculates the correct color values. Now we have the option to store that color value within the ColorPicker and add an additional PREVIEW button to our user interface, on which the user has to click to preview the selected color on for example some random text.
If this is a process, which the user is applying a lot it will be pretty annoying to click every time on the PREVIEW button, just to update the text's color attribute and to see whether the color is okay or not. Wouldn't it be much better if the color on the text would update instantly whenever you change the selection in the color picker? This is quite an improvement to the overall user experience of your application. You reduce a step in the user's workflow and you save a button in the interface.
And it can be realized fairly simple by using listeners (or callbacks). First you need to define a new Interface within your ColorPicker class or as a separate interface file (like a class file) for a more generic use:
Now we have the interface definition, but we also need "something" that actually "holds" our interface. Therefor we add these lines to our ColorPicker class:
Now we have the basic framework for our listener. In addition we add a helper method to our ColorPicker class that calculates the correct color value whenever the user touches the color field and calls the onColorChanged method.
The final piece that is missing now is just our control Activity, that will respond to this change. We need to create a new instance of the OnColorChangedListener, assign it to our instance of the ColorView and implement our callback function onColorChanged, which in this case only applies the received color to myText. But you can implement here whatever you feel like.
I hope this tutorial was easy to understand. If there are still some issues that need to be cleared up please let me know in the comments. Interfaces might look like a bit of a hassle in the actual process of writing them down, but they will help you a lot in writing clearer code and designing the architecture of your application in a more structured way.
I've just launched a new side project of mine:
Bugfender - A modern remote logger tailor-made for mobile development
It's currently free and you can sign up here: https://app.bugfender.com/signup
Any kind of feedback is more than appreciated :-)
Good Tutorial... was really helpful. Thanks.
ReplyDeleteThanks for the Tutorial. It was very helpful and even better than that it's probably the most simple & easy to follow Android tutorial I've seen. Clear and concise, keep up the fantastic work.
ReplyDeleteWow. You are a lifesaver. There aren't many promising Google results to "Android create my own callback" but this was exactly what I needed. I'll have to post back and let you know if this works for separating location (GPS) tracking from my activities... my hypothesis says it will. :)
ReplyDeleteHi
ReplyDeleteNice tutorial , thanks
but I have a doubt :( , Where will the userSelectedNewColor(int x, int y) method be called ??
Hoping for a reply
Thanks in advance
Thanks, you made it very clear.
ReplyDeleteYour Android tutorial is very clear, but I'm still not able to reproduce it and I'm starting to have a headache ;)
ReplyDeleteI really don't blame you, the learning curve of Android development is just insane, and even with very clear explanation, that's sometime still too complex.
Damn.
This is a lovely tutorial. Please continue writing them.
ReplyDeleteVery helpful and nicely explained. Cheers!
ReplyDeleteHi, are there any codes in your post? I can't see anything. Kindly post it. Thank you!
ReplyDelete