Varun
Varun

Reputation: 3311

View.OnClickListener usage as parameter in Android

Interface names do not end with "()".

And from Android documentation View.OnClickListener is defined as interface.

http://developer.android.com/reference/android/view/View.OnClickListener.html

Having said that, when we pass "View.OnClickListener" to a listener as a parameter why do we have to do end it "()" as shown below.

.setOnClickListener(new View.OnClickListener() {

Upvotes: 0

Views: 811

Answers (5)

Raghunandan
Raghunandan

Reputation: 133560

Its a annonymous inner class

Say you have a button

 button.setOnClickListener(new View.OnClickListener() {
  @override
   public void onClick(View v)
   {

   } 

 });

http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html

You can also do as

  button.setOnClickListener(myclicklistener);

  OnClickListener myclickListener = new OnClickListener() // now you know why you ()
  {
      @Override
      public void onClick(View v)
      {

     } 
  };   

Quoting from the java docs

The anonymous class expression consists of the following:

  • The new operator

  • The name of an interface to implement or a class to extend.

  • Parentheses that contain the arguments to a constructor, just like a normal class instance creation expression. Note: When you implement
    an interface, there is no constructor, so you use an empty pair of
    parentheses, as in this example.

  • A body, which is a class declaration body. More specifically, in the body, method declarations are allowed but statements are not.

Upvotes: 1

Suresh Atta
Suresh Atta

Reputation: 121998

It's syntax of Anonymous inner class, which enable you to declare and instantiate a class at the same time.

.setOnClickListener(new View.OnClickListener() {

//all methods in the interface  implemented here

});

That setOnClickListener taking an object of type implemented by OnClickListener,Instead of creating an object with that interface,directly implementing the methods there.

Upvotes: 4

Daniel Lerps
Daniel Lerps

Reputation: 5375

Because you are creating an Object which implements that particular interface. It is a constructor call on an anonymous inner class which you define via the method in the block that follows.

something.setOnClickListener(new View.OnClickListener()
{
   @Override
   public void onClick(View invoker)
   {
       // this is a method specified by the interface. The new Object will have this method implementation
   }
});

Upvotes: 1

Martynas Jurkus
Martynas Jurkus

Reputation: 9301

Because we're initiating anonymous class here. Thus an interface becomes an object which is passed as a parameter.

You coul do this way:

View.OnClickListener listener = new View.OnClickListener() {

    @Override
    public void onClick() {
    }
};
someView.setOnClickListener(listener);

Upvotes: 1

Johnson Smith
Johnson Smith

Reputation: 201

On click listener is an interface.

Syntax

variable.setOnClicklistener(new View.OnClickListener()
{})

This is the way we can use onClicklistener.

Upvotes: 1

Related Questions