pronay biswas
pronay biswas

Reputation: 45

query about View.OnClickListener() in android

I'm trying to understand what is View.OnClickListener().
I have read this site: http://developer.android.com/reference/android/view/View.html, but I cannot understand who is the client and who is the listener.

Please explain in details. Thanks in advance.

Upvotes: 0

Views: 232

Answers (2)

Simon Dorociak
Simon Dorociak

Reputation: 33505

From docs:

Interface definition for a callback to be invoked when a view is clicked.

reference

Simply said: So when you implement this, you are able to handle click events for your Views - all widgets as Button, ImageView etc..

When you implement this you have to implement onClick method. When you click on some View, this method is immediately called.

public void onClick(View v) {
   switch(v.getId()) {
      // do your work
   }
}

But don't forget that you have to register your OnClickListener for specific widget

someButton.setOnClickListener(this);

Most likely you need to learn Android basics and i recommend it to you.

Note: You can use Listeners also as anonymous classes

Upvotes: 1

fdomig
fdomig

Reputation: 4457

This is an Interface to implement for classes which want to get a notification if a View element got clicked.

For instance:

public class FooActivity extends Activity implements View.OnClickListener {

    public void onCreate(...) {
        View v = findViewById(...);
        v.setOnClickListener(this);
    }

    public void onClick(View v) {
        // method which is invoked when the specific view was clicked
    }
}

Upvotes: 0

Related Questions