4

Apologies for my title, I am having trouble properly articulating the problem.

I have seen OnCLickListener implemented in two ways. The first is done by signifying that your class implements OnCLickListener. The second accomplishes the task by having you declare it yourself.

Why in the first option can you simply put this as your setOnCLickListener argument, but in the second you must go through the trouble of creating the OnClickListener object yourself?

The first:

public class WidgetConfig extends Activity implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.widgetconfig);
    Button b = (Button)findViewById(R.id.bwidgetconfig);
    b.setOnClickListener(this);
    }
    //onClick defined outside of the onCreate
    @Override
    public void onClick(View arg0) {
    // TODO Auto-generated method stub

    }

The Second:

public class WidgetConfig extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.widgetconfig);
    Button b = (Button)findViewById(R.id.bwidgetconfig);
    b.setOnClickListener(bListener);
}



private Button bListener = new OnClickListener(){

b.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {

            //TO-DO 

            }
});
Michal Borek
  • 4,584
  • 2
  • 30
  • 40
Dick Lucas
  • 12,289
  • 14
  • 49
  • 76
  • this question is nowhere related to android, it related to your JAVA concepts – Kapil Vats Jul 09 '13 at 04:39
  • Way #3 - Just FYI, you can also create your own custom class, have it implement the ClickListener interface, and then create a new instance of that listener. You then call: setOnClickListener(new YourCustomClass()). It's a good way to encapsulate the code which handles your click actions. – Swifty McSwifterton Jul 09 '13 at 05:07

5 Answers5

4

In the first method, you entire Activity class implements the OnClickListener interface. You can set the OnClickListener of every View to this, and receive all the click events in one method, where you can then filter them and act upon them.

The second method uses an anonymous inner class that implements the interface method. By using this approach, you receive events only for that particular View.

In the first method, your entire class uses one single instance of the OnClickListener, that is passed to all the Views you want to listen for clicks on.

The second method translates to:

Button.OnClickListener anonymous_listener = new Button.OnClickListener() { ... };
button.setOnClickListener(anonymous_listener);

Which is to say that it dynamically creates and stores a new OnClickListener instance when you use it.

ridoy
  • 6,274
  • 2
  • 29
  • 60
  • If the whole class implements OnCLickListener, as in the first method, how is it that I would set multiple onClick methods for multiple buttons? – Dick Lucas Jul 09 '13 at 20:15
  • You would have to use a **switch** statement within the overridden method, and use the **view id** to switch on. Just look up **switch statement** for java and you will see. – Swifty McSwifterton Jul 10 '13 at 03:14
  • Which is better approach and Why?? – codeRider Jul 22 '14 at 13:38
  • I think you should read this for better understanding: http://programmers.stackexchange.com/questions/110106/what-is-the-proper-way-to-implement-the-onclicklistener-interface-for-many-butto – Shankari vatsalkumar Aug 06 '14 at 06:29
1

I usually do the first manner because it saves an object. But if you need to implement many listeners, then to keep code more organized and neat, you can consider the second manner.

TieDad
  • 9,143
  • 5
  • 32
  • 58
1

In Case 1 this represents an object of type Activity and OnClickListener. So, when you pass this in b.setOnClickListener(this) it represents object of type '`OnClickListener'.

In Case 2 you are passing an anonymous object of type OnClickListener. Since, OnClickListener is an interface you have to define the method onClick inside the anonymous object.

Krrishnaaaa
  • 689
  • 11
  • 23
1

If your onClickListener needs to access (most of the) methods and member variables of your Activity class, then, using the first approach can be taken to simplify the code. Also, it can be preferred if you want to handle all the "onClick"s in same manner.

Otherwise, second option should be taken when you have lot of buttons (or other type of views requiring click handling) and all of them need to be handled in different way. In that case, it does not make sense to have your Activity handle onClick for one button (view) and rest of them are handled by individual onClickListener's as that makes code look inconsistent.

ridoy
  • 6,274
  • 2
  • 29
  • 60
Wand Maker
  • 18,476
  • 8
  • 53
  • 87
1

Your WidgetConfig class "is an" Activity due to the fact that it extends Activity. However, your WidgetConfig class "is (also) an" OnClickListener because it implements the interface. Thus, when setOnClickListener() asks for you to pass in a click listener in as an argument, you can pass in "this" because "this" (which is WidgetConfig) "is an" onClickListener.

With an anonymous inner class you get the setting and the call back all in one. You should read this, though, about the potential pitfalls of using such classes: When exactly is it leak safe to use (anonymous) inner classes?

Community
  • 1
  • 1
Swifty McSwifterton
  • 2,637
  • 1
  • 30
  • 37