1

I have some ImageButtons I want to use both on, I have already implemented onTouch like this

addAppointment.setOnTouchListener(new MyTouchListener());

and I got this:

private final class MyTouchListener implements View.OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            vID = view;
            ClipData data = ClipData.newPlainText("", "");
            View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(data, shadowBuilder, view, 0);

            return true;
        } else {
            return false;
        }
    }
}

class MyDragListener implements View.OnDragListener {

        @Override
        public boolean onDrag(View v, DragEvent event) {
            int action = event.getAction();
            switch (event.getAction()) {
                case DragEvent.ACTION_DRAG_STARTED:

                    break;
                case DragEvent.ACTION_DRAG_ENTERED:

                    break;
                case DragEvent.ACTION_DRAG_EXITED:

                    break;
                case DragEvent.ACTION_DROP:
                    eventLocation = (int) event.getX();

                    break;
                case DragEvent.ACTION_DRAG_ENDED:

                    if (event.getResult() == true) {
                        addTreatment(vID, eventLocation);
                        System.out.println(vID);
                    }else{
                        addTreatment(vID, 0);
                    }

                default:
                    break;
            }
            return true;
        }
    }

this works flawless! but i also want to add OnClickListener because its supposed to do a different action if you just single click the button. But when i set a click listener it doesn't work at all it doesn't fire the Click at all.

Tim
  • 41,901
  • 18
  • 127
  • 145
Robinhiio
  • 105
  • 1
  • 11
  • check this http://stackoverflow.com/questions/8786047/can-we-give-both-ontouchlistener-event-and-onclicklistener-on-a-single-text-view?rq=1 – Piyush May 31 '16 at 10:03

1 Answers1

0

If you return true from onTouch, you're telling android that you handled the action and it doesn't have to do anything after you anymore.

If you want android to further process the action, which also involves passing it to an onClick handler, you have to return false from onTouch after you handled it.

public boolean onTouch(View view, MotionEvent motionEvent) {
    if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        vID = view;
        ClipData data = ClipData.newPlainText("", "");
        View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
        view.startDrag(data, shadowBuilder, view, 0);

        return false; // here
    } else {
        return false;
    }
}

Also see What is meaning of boolean value returned from an event-handling method in Android where this is explained in more detail.

Community
  • 1
  • 1
Tim
  • 41,901
  • 18
  • 127
  • 145