8

Is View.OnClickListener() a function or interface? When we try to set a onclicklistener() method in android, we use new View.OnClickListener() and it bugs me there cuz as far as I know,

  • we don't need to initialize an object of class containing static method inorder to use those methods. Why we do this?
  • When we use implements inorder to implement an interface, we don't call the static methods of the interface.

So can some one tell me why do we do:

  • new View.OnClickListener(), for using onclick() method?
  • Why do we use () with View.OnClickListener if it is an interface?

Thanks for your reply..

Alfred James
  • 1,029
  • 6
  • 17
  • 27

2 Answers2

19

I'm not sure I understand what you are writing about static methods. View.OnClickListener is an interface: http://developer.android.com/reference/android/view/View.OnClickListener.html

To set a click listener on a view, you pass an instance implementing the OnClickListerner interface: http://developer.android.com/reference/android/view/View.html#setOnClickListener(android.view.View.OnClickListener)

The most common way to do this in android is to define an anonymous inner class (http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html) that implements OnClickListener like

myView.setOnClickListener(new View.OnClickListener() {
    @Override           
    public void onClick(View v) {
        // Handle view click here           
    }
});

The code above both defines an anonymous inner class and creates an instance of it. It is equivalent to first defining a class implementing View.OnClickListener (if defined in the same class)

class MyOnClickListener implements View.OnClickListener {
    @Override           
    public void onClick(View v) {
        // Handle view click here           
    }
}

And later using this

MyOnClickListener listener = new MyOnClickListener();
myView.setOnClickListener(listener);
toucan
  • 1,465
  • 1
  • 17
  • 31
  • One thing that I didn't understand is why do we use () when using IMPLEMENTS for using the interface. – Alfred James Aug 05 '12 at 09:43
  • 4
    () is the call to the constructor of the anonymous inner class, i.e., where the object implementing OnClickListener is created. Google for "anonymous inner class" or have a look here http://stackoverflow.com/questions/355167/how-are-anonymous-inner-classes-used-in-java or here http://viralpatel.net/blogs/inner-classes-in-java/ – toucan Aug 05 '12 at 10:00
  • Thanks for the important concepts. – Hamzeh Soboh Sep 09 '13 at 07:50
3

enter image description here

Sample Code,

Internally it works something like this,

public class MyView{

public stinterface MyInterface{
        public void myOnClick(View view);
    }

}

 public class MyButton{
        View view;

        public void setOnClicker(MyInterface onClicker) {
            onClicker.myOnClick(view);
        }
    }


public class MyExample{

    public void method(){
        MyButton myButton = new MyButton();
        myButton.setOnClicker(new MyInterface() {
            @Override
            public void myOnClick(View view) {

            }
        });
    }
}