Alone89
Alone89

Reputation: 307

Keeping multiple OnClickListeners in one class

I am kinda new to Android and it would improve my application a lof if I coul keep several OnClickListenres in one class. What I am thiking of is something like this :

Public class OnClickListeners {

public Button.OnClickListener open;
public Button.OnClickListener doSomethingElse;
public Button.OnClickListener etc;

    public OnClickListeners() {
        open = new Button.OnClickListener()
        {
                public void onClick(View view)
                {
                    DetailList.SetId(view.getId());
                    Intent intent = new Intent(view.getContext(), DetailList.class);
                    startActivityForResult(intent, 100);
                }
        };
    }
}

So I can then reference it in other class B like this

button1.setOnClickListener(OnClickListeners.open);

Any though how to do it? Android SDK seems to be against me as I can figure it out now for about 2 days now...

Thanks for any advices and help

Upvotes: 2

Views: 1253

Answers (5)

As none of the solutions actually did what I wanted to achieve - I needed a second (or multiple) onClickListener that did not override the onClickListeners that were already assigned to the control.

Here is the java class that I wrote for that purpose:

https://gist.github.com/kosiara/c090dcd684ec6fb2ac42#file-doubleclicklistenerimagebutton-java

public class DoubleClickListenerImageButton extends ImageButton {

    View.OnClickListener mSecondOnClickListener;

    public DoubleClickListenerImageButton(Context context) {
       super(context);
    }

    [...]

    public void setSecondOnClickListener(View.OnClickListener l) {
        mSecondOnClickListener = l;
    }

    @Override
    public boolean performClick() {
       if (mSecondOnClickListener != null)
          mSecondOnClickListener.onClick(this);
       return super.performClick();
    }

    @Override
    public boolean performContextClick() {
       if (mSecondOnClickListener != null)
          mSecondOnClickListener.onClick(this);
       return super.performContextClick();
    }
}

Upvotes: 0

Ian
Ian

Reputation: 3520

There is a sleek way to consolidate all your anonymous classes into one and switch on the view. This works best if you know ahead of time which buttons will be using the clicklistener :

public class AndroidTestClickListenerActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new MyClickListener());

        Button button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new MyClickListener());

        Button button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener(new MyClickListener());
    }


}

class MyClickListener implements View.OnClickListener {
    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
            case R.id.button1:
                // do soemthign for button1
                break;
            case R.id.button2:
                // do something for button2
                break;
            case R.id.button3:
                // do something for button3
                break;
            default:
                // do something for any other button

        }

    }
}

Upvotes: 1

Daniel Novak
Daniel Novak

Reputation: 2756

You can, but you have to declare the OnClickListener as static if you would like to use it in this manner.

public static Button.OnClickListener openListener = new Button.OnClickListener() {
     public void onClick(View view) {

     }
};

Then you can use:

button1.setOnClickListener(OnClickListeners.openListener);

As noted by other user - this approach is most like bad. You should handle view listeners on the same view and then maybe call another method like openActivity(). I would not do this - you are also openning an activity from another activity, this will probably don't work at all or will mess up the activity history stack

Upvotes: 0

Peter Knego
Peter Knego

Reputation: 80330

The problem of you approach is that usually listeners have to manipulate some data that is part of the class where UI elements are.

If you take listeners out and put them in a separate class, you will also have to provide a lot of references to objects where data to be manipulated is. This will create a lot of interdependent classes, which will not be nice.

IMHO the cleanest way is to use anonymous inner classes.

Upvotes: 0

ernazm
ernazm

Reputation: 9258

You can write

button1.setOnClickListener(new OnClickListeners().open);  

instead, but this seems an odd architecture for me. I'd suggest you to keep 1 listener in 1 file, having all of them in 1 package, and use like

button1.setOnClickListener(new OpenListener());  

Upvotes: 0

Related Questions