2

I am new to android development and I realized it's much more harder to use all the interfaces in a real world examples than using them in just sample codes which are trying to show you how to use the interface.

Since I would like to understand every single line I type I will start with this :

Button clearButton = (Button) findViewById(R.id.buttonClear);

clearButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {   
    }
});

The first line is easy, I just assign a button to button object based on its id from xml but the listener I don't understand, simply I get the clearButton object and I will use one of its method, setOnClickListener, and then in the argument I pass anonymous class which behavior I would like to override, but View.OnClickListener() is method not an object so? I'm writing a class inside a function?

new View.OnClickListener() {
    @Override
    public void onClick(View v) {   
    }

this looks like it's a function OnClickListener which contains a inline class so ?

ekad
  • 14,436
  • 26
  • 44
  • 46
  • Please look at the [Java tutorial about anonymous classes](https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html). – SOFe Sep 18 '16 at 17:25
  • `View.OnClickListener()` is a constructor, which you're calling on the anonymous class. – 4castle Sep 18 '16 at 17:26
  • and im passing a new class inside constructor? –  Sep 18 '16 at 17:26
  • Correct. And if `View.OnClickListener` was an (abstract?) class with parameters in the constructor, you would put your parameters inside the parentheses. Think of it like making a call to `super()`. – 4castle Sep 18 '16 at 17:28

1 Answers1

2

It's called observer pattern. You register your listener to the UI and tell it to call your code when something happens; in this case that something is user clicking your clear button.

SO:

This is simple, you are creating a button object and attaching it your layout file.

Button clearButton = (Button) findViewById(R.id.buttonClear);

Next:

Let me rewrite this:

clearButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {   
        }
    });

to this:

//First let's create an implementation of this interface.
// These are also refereed to as callback interfaces as 
//the methods in their implementation are called as whenever 
//something happens on the UI. In this call onClick is the callback    method.


private class MyButtonClicklistener implements View.OnClickListener
{
  @Override
            public void onClick(View v)
 {
//Do something on the button click   
            }

}

Create instance of your listener

MyButtonClickListener mListener = new MyButtonClickListener();

Finally register your Listener. Now you are telling your UI to call onClick method of mListener object whenever someone clicks on the clear button.

clearButton.setOnClickListener(mListener);
java_doctor_101
  • 3,287
  • 4
  • 46
  • 78
  • i have never seen an interface which contains dot in its name , i dont understand that implements View.OnClickListener its like implementing exact variable for example int from view interface ? –  Sep 18 '16 at 17:57
  • Have a look at this answer http://stackoverflow.com/a/6947033/1099142 and specially the comment for this answer. – java_doctor_101 Sep 18 '16 at 23:49