1

I'm new to Java and Android Development in General. I found a sample which uses the following statement.

sButton.setOnClickListener(this);

in the onCreate() method of the Activity which implements View.OnClickListener where sButton is a Button variable. As far as I understand this register the on click event handler Later in the sample

 public void onClick(View v) {
    if(v.getId() == R.id.button_s)
    {
    //some work
    }

this happens. My question is if it uses the 'this' keyword from inside the activity shouldn't it pass an object of class Activity? If that happens then the Button ID would never match.

I know there are other methods to implement the Button click.

I have a bit of experience with C# and Windows Phone. The procedure there was that the methods were called for the respective buttons without any need for registering them.

Also what is the difference between event handlers and listeners?

Any help would be appreciated! Thanks

user3927312
  • 814
  • 2
  • 13
  • 27
  • Your Activity implements ClickListener , it will be passed to the `setOnClickListener()` method , the view passed to the `onClick(View v)` is the view that is clicked – Abdelrahman Elkady Jan 15 '15 at 11:56

3 Answers3

2

The OnClickListener (in this case the Activity instance) is used to declare the behaviour of your application when it consumes click event. However, your application's Main (UI) Thread registers all (UI) events and it will dispatch the appropriate View object as a parameter to your onClick(View v) function.

The snippet you provided - sButton.setOnClickListener(this) simply instructs your application to use the implemented OnClickListener inside your Activity implementation to respond to user clicks. It doesn't forward the this instance as the parameter to the onClick() function, Android OS does that.

In conclusion: The View v parameter in your onClick(View v) function will correspond to the View that the user clicked, regardless of the OnClickListener that has been attached to that View

EDIT: This is (perhaps outdated but) Android's source-code for the performClick() method of the View class. As you can see, inside that method it calls the mOnClickListener.onClick(this) if there's a listener attached, and that's how the view that has been clicked is forwarded to the onClick() method of the appropriate onClickListener object.

nstosic
  • 2,584
  • 1
  • 17
  • 21
  • Could you paste any links which would provide more information on the way this actually works? As I understand it from your answer the OS sends the Required view which is activated and hides how this actually happens in my code. Is this true? – user3927312 Jan 15 '15 at 13:17
  • Updated answer with link to the Android's source-code, hopefully it helps – nstosic Jan 15 '15 at 13:25
1

One is below

Button btn_stop=(Button) findViewById(R.id.btn_stop);
btn_stop.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                }
            }
        });

Second is through XML android:onClick="doClick"

below is code for activity

public void doClick(View v)
{

}
droidd
  • 1,361
  • 10
  • 15
1

When you do this

sButton.setOnClickListener(this);

this is indeed an instance of Activity. But it is declared as

MyActivity extends Activity implements View.OnClickListener

The important part is that it implements the interface that setOnClickListener takes as argument. This is why it works, this is treated as an OnClickListener, regardless of being an Activity or not.

As for the difference between event handlers and listeners, see this question.

Community
  • 1
  • 1
2Dee
  • 8,609
  • 7
  • 42
  • 53
  • Thanks for the link. I had come across it before posting the question, however it still doesn't really give concrete differences between them. – user3927312 Jan 15 '15 at 13:09