Skip to content Skip to sidebar Skip to footer

Single Method To Implement Ontouchlistener() For Multiple Buttons

I am trying to see if there is a way to create a single method to implement a touch listener for multiple buttons, seeing as I have quite a few buttons that do almost exactly the s

Solution 1:

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

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

publicclassMyTouchListenerimplementsOnTouchListener {
    @OverridepublicbooleanonTouch(View v, MotionEvent event) {
        switch(v.getId()){
            case1:
                //do stuff for button 1break;
            case2:
                //do stuff for button 2break;
            case3:
                //do stuff for button 3break;
            case4:
                //do stuff for button 4break;
        }
        returntrue;
    }

}

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.

publicclassMyActivityextendsActivityimplementsOnTouchListener {

    voidinitialization(){
        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);
    }

    @OverridepublicbooleanonTouch(View v, MotionEvent event) {
        switch(v.getId()){
            case1:
                //do stuff for button 1break;
            case2:
                //do stuff for button 2break;
            case3:
                //do stuff for button 3break;
            case4:
                //do stuff for button 4break;
        }
        returntrue;
    }

}

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

Solution 2:

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)--
            returntrue;
        case MotionEvent.ACTION_UP:
            --(Your ACTION on Release)--
            returntrue;
    }
    returntrue;
}

Solution 3:

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:

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

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

        default:
            break;
        }
    }
    returntrue;
}

Solution 4:

OnTouchListenermOnTouchListener=newOnTouchListener() {

    @OverridepublicbooleanonTouch(View v, MotionEvent event) {
        // Code goes herereturntrue;
    }
};

button1.setOnTouchListener(mOnTouchListener);
button2.setOnTouchListener(mOnTouchListener);

Solution 5:

I merge two answer, this is my code

publicclassMyActivityextendsActivityimplementsOnTouchListener {
Button mGreen, mRed;

voidinitialization() {

    ... //do initialization stuff

    mGreen.setOnTouchListener(this);
    mRed.setOnTouchListener(this);
}

@OverridepublicbooleanonTouch(View v, MotionEvent event) {


    switch (event.getAction() & MotionEvent.ACTION_MASK) {
        caseMotionEvent.ACTION_DOWN:
            actionDown();
            break;
        caseMotionEvent.ACTION_UP:
            actionUp();
            break;

        caseMotionEvent.ACTION_POINTER_DOWN:
            break;

        caseMotionEvent.ACTION_POINTER_UP:
            break;

        caseMotionEvent.ACTION_MOVE:
            actionMove();
            break;
    }

    returntrue;
}

publicvoidactionDown() {
    switch (view.getId()) {
        case R.id.button_green:
            //todobreak;

        case R.id.button_red:
            //todobreak;
    }
}

publicvoidactionUp() {
    switch (view.getId()) {
        case R.id.button_green:
            //todobreak;


        case R.id.button_red:
            //todobreak;
    }
}

publicvoidactionMove() {
    switch (view.getId()) {
        case R.id.button_green:
            // todobreak;

        case R.id.button_red:
            // todobreak;
    }

}}

I hope that this code will help someone

Post a Comment for "Single Method To Implement Ontouchlistener() For Multiple Buttons"