1

I looked on http://developer.android.com/reference/android/view/package-summary.html and saw that the view class has an interface named "View.OnClickListener" which is "Interface definition for a callback to be invoked when a view is clicked" My question is what the difference is if you specify the view or not in the interface?

Basically is

button.setOnClickListener(new Button.OnClickListener() the Same as

button.setOnClickListener(new OnClickListener()?

committedandroider
  • 8,711
  • 14
  • 71
  • 126

1 Answers1

4

There are 2 of setOnClickListener one for the View class and one refer to DialogInterface Class.

So to in order to manipulate the View like a Button or ImageView and add an action to it, you need to use View.OnClickListener while dealing with Dialog buttons you should use DialogIneterface.onClickListener both have different arguments.

Usually by adding onClickListener, the View Class will be imported by default or it will make you choose between both classes. so you don't need to add View.onClickListener. However, if the class DialogInterface have been imported already and you want to use the View onClickListener then you have to write View.onClickListener to differentiate both classes' onClickListener.

Hope it is clear now and this is what you are looking for.

Coderji
  • 7,655
  • 5
  • 37
  • 51
  • does it have something to do with this keyword? I know that when you call an instance method inside another instance method, you dont have to put the this before the method call(done automatically, refers to the current executing object) But here its an anonymous class. Does it work the same way? Button, type of the view, is just put on automatically? – committedandroider Jun 26 '14 at 02:25
  • 1
    the onClickListener is a bit different case than `this` keyword, you don't have to put View before onClickListener as long as you have imported the `View` class, however if you didn't import it then you need to add the `View` keyword. for `this` keyword the `this` in outer class isn't the same of the `this` in the `anonymous` class you can call the `this` of outer class by `OuterClassName.this`, you can refer to this [link](http://tomlee.co/2007/06/java-this-and-inneranonymous-classes/) for `this` keyword between outer and inner class. – Coderji Jun 26 '14 at 02:34