Single method to implement onTouchListener() for multiple buttons

Yes you are right, there is a better way. A single TouchListener that handles everything, determining which button it is via the id.

void intialization(){
     Button m1, m2, m3, m4;
     ... //do initialization stuff
     m1.setId(1);
     m2.setId(2);
     m3.setId(3);
     m4.setId(4);
     MyTouchListener touchListener = new MyTouchListener();
     m1.setOnTouchListener(touchListener);
     m2.setOnTouchListener(touchListener);
     m3.setOnTouchListener(touchListener);
     m4.setOnTouchListener(touchListener);
 }

public class MyTouchListener implements OnTouchListener {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch(v.getId()){
            case 1:
                //do stuff for button 1
                break;
            case 2:
                //do stuff for button 2
                break;
            case 3:
                //do stuff for button 3
                break;
            case 4:
                //do stuff for button 4
                break;
        }
        return true;
    }

}

And that's how you'd do it! A numerical approach for the id's is very helpful in this case. Another approach is to have your activity implement the OnTouchListener in your activity, and then your code would be even simpler.

public class MyActivity extends Activity implements OnTouchListener {

    void initialization(){
        Button m1, m2, m3, m4;
        ... //do initialization stuff
        m1.setId(1);
        m2.setId(2);
        m3.setId(3);
        m4.setId(4);
        m1.setOnTouchListener(this);
        m2.setOnTouchListener(this);
        m3.setOnTouchListener(this);
        m4.setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch(v.getId()){
            case 1:
                //do stuff for button 1
                break;
            case 2:
                //do stuff for button 2
                break;
            case 3:
                //do stuff for button 3
                break;
            case 4:
                //do stuff for button 4
                break;
        }
        return true;
    }

}

Note: This approach also will also work for OnClickListener, OnCheckedChangeListener, or any other listener that you would set on an Android view.


With ButterKnife it would be like this. (in my case ImageView as buttons)

@OnTouch({R.id.Button1, R.id.Button2, R.id.Button3})
public boolean buttonsTouched(ImageView button, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            --(Your ACTION on Pressed)--
            return true;
        case MotionEvent.ACTION_UP:
            --(Your ACTION on Release)--
            return true;
    }
    return true;
}

Yeah, there is a better approach of doing the same.
1) Make your class implement OnTouchListener.
2) Add this listener to every button that should handle touch event. Like this:

button1.setOnTouchListener(this);

3) And in this public boolean onTouch(View arg0, MotionEvent arg1) {});

method you can use switch case on the view that was touched . The first argument i.e. arg0 is the view the touch event has been dispatched to. In your case it will be different buttons. Something like this:

public boolean onTouch(View arg0, MotionEvent arg1) {
    if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
        switch (arg0.getId()) {
        case R.id.button1: // Id of the button
            // Your code goes here
            break;

        case R.id.button2: // Id of the button
            // Your code goes here
            break;

        default:
            break;
        }
    }
    return true;
}