3

I'm setting up a Event Listener on a Button in Android Studio for changing the text inside the TextView and came to this point were we define OnClickmethod. In its parameter list it ask for View Object. Can anyone explain what it does? I can't understand where the object of View class is going to get used.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Eddy
  • 344
  • 5
  • 16

2 Answers2

4

It is there to let you reuse event handlers like the OnClick method, the View parameter is in your case the Button instance that has fired the method - multiple Buttons can have the same OnClick handler, inside the method you can check which of the Buttons has fired (if there are more than one) and react accordingly.

Actually it is very typical for event-driven programming not only on Android but all contemporary UI programming - iOS, Windows, OS/X, etc.

There is a little bit more to that: the parameter's type is View and not Button because not only Buttons react to OnClick and the common type of UI objects reacting to OnClick is View.

Now not only you can handle the event differently, you have also direct access to the source of the event so that you can work on it - in the example of the Button you can change its caption and you know for sure that you are changing the caption of the very UI object that fired the event.

So it has a lot of sense to do event-driven APIs in this manner.

UPDATE

One of the methods to register an event handler (or ClickListener in Android-world) is assigning the method name to the onClick Attribute directly in the layout - provided of course that the method has the proper signature, that is expect one Parameter of type View and has void return type - you can see an example in the other answer.

The other is to assign an anonymous inner class as a Listener like this:

findViewById(R.id.someButton).setOnClickListener(
    new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        doSomething();
      }

    });

You can also have an instance of a class implementing the View.OnClickListener interface, even the `Activity' itself could do it, and then register it as a listener.

In fact all of them work the same - there is an instance of a class implementing View.OnClickListener interface registered in the button instance, the button (or a view for that matter) recognizes a click inside its boundaries and calls a listener passing itself (this) to the listener.

Im my personal opinion the third way is the worst - you can only have one method of a name in the class but many buttons in a layout, so all of them would need to be handled using conditional code.

The first is nice because you can set it visually, yet given that it is all in XML files and you could have multiple layouts for an activity it can be quite a mess to keep it under control in a larger project.

My favorite is thus the second one - it allows you to keep all the logic on one place in the code, for example if you register all you handlers in the onCreate method of an activity.

Tomasz Stanczak
  • 12,796
  • 1
  • 30
  • 32
  • Thanks Alot mate I got it now :) – Eddy Jun 07 '16 at 15:35
  • But I got one last question - since onClick is a method which we are using for button and using View as parameter it means whenever it is called, a object is passed to it (Button class object if I'm not wrong). I can't understand where it is passing the object(Button class Object) as parameter to this method can you explain me? – Eddy Jun 09 '16 at 14:10
  • I'm not quite sure what you mean, I'll try to explain what I suppose - every `View` handles it's clicks events on its own and calls a registered listener method (event handler) passing `this` to the listener. So it is the Button itself calling your method and using `this` as a parameter. – Tomasz Stanczak Jun 09 '16 at 17:28
  • I'll add an example to the answer in a moment. – Tomasz Stanczak Jun 09 '16 at 17:29
  • I got it what you mean but I didn't mean this by my question :P - I mean the parameter in onClick method "public void onClick(View v)" <- here how came that View Object 'v' points to the button on which we are using this method, that's what I'm actually trying to say, let me know if you didn't get it I will put a example. – Eddy Jun 09 '16 at 20:31
  • Because each Button is also a View, so a Buton that reacts to the click and calls the method uses itself as a parameter to the method. In the Method if you check the class of the parameter you'll see that it a Button class and the Button instance you had registered your method as a listerner on. – Tomasz Stanczak Jun 10 '16 at 09:30
  • Alright man I got it now properly - Thanks for replying to my question :) – Eddy Jun 10 '16 at 11:15
4

Using view parameter to find the caller, see code:

public void buttonOnClick(View view) {
    switch (view.getId()) {
      case R.id.button1:
        doSomething1();
        break;
      case R.id.button2:
        doSomething2();
        break;
      }
}

So it is possible to use the same method on multiple objects:

android:onClick="buttonOnClick"
Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
wake-0
  • 3,918
  • 5
  • 28
  • 45