I am trying to learn android programming and I am stuck at one point, in terms of deeply understanding the concept.
So there is this code which listens the click event for a view (a button). Code is from a site by the way, and working just fine. But I am obsessed with understanding it to the nuts and bolts.
clearButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// do some stuff
}
});
I have questions in some different levels:
1. So the parent function (clearButton.setOnClickListener) is the event handler, as far as I can tell, which is trigerred when the button is clicked. And yet we are passing it an object's method, which, as far as I can tell, another event listener. Both of them are named as "ClickListener" so I am confused. Are two of them really event listeners?
2. Where did the View object (one which passed as the callback) came from? Is it implicitly created by the system? I have no such definition in my MainActivity.java file.
3. I clumsly tried to write it with parent function invoking another simple function which just displays an AlertDialog, but I was badly failed, since clearButton.setOnClickEventListener expects a View to be returned from callback (I think:)). Is there any other way to accomplish it or this is the only way setting an event listener to a view? Do I have to accept it the way it is and embrace it, instead of struggling with it :) I ask this only to understand the logic of the structure, code works just fine.
4. What is the view passed to the View.OnClickListener function? Is it the current view (I doubt it is)? What is the use case of the view passed to the View.
5. Where can I (or can I) review the code of the View.OnClickListener function to better understand it?
6. All this exercise trigerred another question in my mind about callbacks, but it is probably subject of another question altogether :)