One OnClickHandler for multiple Buttons
Asked Answered
T

6

46

I find myself doing things like this all the time:

    Button button1 = (Button) findViewById(R.id.button1);
    Button button2 = (Button) findViewById(R.id.button2);
    Button button3 = (Button) findViewById(R.id.button3);

    button1.setOnClickListener(menuButtonListener);
    button2.setOnClickListener(menuButtonListener);
    button3.setOnClickListener(menuButtonListener);
...

and

   private OnClickListener myButtonListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
      switch(v.getId()){
       case R.id.button1 :
    ...

Is there a better way to set the OnClickListener?

Tsar answered 25/9, 2010 at 20:24 Comment(0)
T
59

You can also set it in your layout xml using the android:onclick attribute.

android:onClick="onClick"

Then in your activity class add the onClick method.

public void onClick(View v) {
...

Here's the documentation.

Troth answered 25/9, 2010 at 20:30 Comment(3)
Ah, nice :) Short and effective. I like it!Tsar
What if the buttons are created dynamically?Ezraezri
@Ezraezri Both this approach and mseo's approach will work for dynamically created buttons too.Schoenburg
U
35

Have your class implement `View.OnClickListener', like

public class MyActivity extends Activity implements View.OnClickListener {

    Button button1, button2, button3;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate();

        ...

        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
   }

   @Override
   public void onClick(View v) {
       switch(v.getId()) {
           case R.id.button1:
           // do stuff;
           break;
           case R.id.button2:
           // do stuff;
           break;
       ...
   }
}
Until answered 25/9, 2010 at 20:33 Comment(1)
Well ... doesn't save me a lot of work. It's another possibility to do it, but it isn't really shorter or more elegant than my approach.Tsar
M
5

Gettin @foenix answer, you can simply do something like:

    int[] buttons = {R.id.button1, R.id.button2, R.id.button3, R.id.button4, R.id.button5, R.id.button6, R.id.button7
            , R.id.button8, R.id.button9};
    for (int i = 0; i < buttons.length; i++) {
        Button buttonNum = (Button) rootView.findViewById(buttons[i]);
        buttonNum.setOnClickListener(doSomething);
    }

private OnClickListener doSomething= new OnClickListener() {
     @Override
     public void onClick(View v) {
            //doSomething
   }
};
Mudlark answered 28/3, 2014 at 18:35 Comment(0)
N
4

It should be noted, that

android:onclick="onClick"

requires at least Andoid SDK 1.6. So if you want your App to be accessible to as big audience as possible, you probably want to write your App against Android 1.6. Unless your App requires a feature which is only available in 1.6+. So while your first attempt is bit more of work, it has greater backwards compatibility. I usually do it the way you do, to keep my Apps 1.5 comptaible.

Edit: It's easy to overlook this, if you set up Android 2.2 as SDK in your projects settings, but set minSDK version to 3 (1.5), as it doesn't give compiler errors.

Node answered 25/9, 2010 at 22:15 Comment(0)
H
1

I think in case, when your buttons are not in Activity but in FragmentDialog, etc., this can help

Context mContext = getActivity().getBaseContext();
mRes = mContext.getResources();
String[] idOfButtons = { "button1", "button2", "button3"};
for (int pos = 0; pos < idOfButtons.length; pos++) {
   Integer btnId = mRes.getIdentifier(idOfButtons[pos], "id",(getActivity()).getBaseContext().getPackageName());
    ImageButton ib = (ImageButton) v.findViewById(btnId);
    ib.setOnClickListener(this.onClickNum);
}
Headpiece answered 28/11, 2012 at 18:25 Comment(0)
R
0

If you want to do less code for click listeners you have this lib [http://jakewharton.github.io/butterknife/]

you just have to do this

 @OnClick({ R.id.button1, R.id.button2, R.id.button3 })
public void doSomething(View button) {
    //do whatever you want
    button.changeWhateverInTheView();
}
Ragen answered 2/2, 2018 at 18:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.