1

I would like an android button to be triggered if a user drags across it and releases his finger on it in addition to the standard activation if it is clicked and released.

I was trying to use the ACTION_MOVE MotionEvent, but this event is only triggered between ACTION_DOWN and ACTION_UP events. I also think that ACTION_OUTSIDE might be a solution. Is there a way to do this?

Here is my code with the actions taken stripped out (b is a button):

b.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {

                if (event.getAction()== MotionEvent.ACTION_DOWN){
                    //Tiggers correctly
                } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                    //Triggers correctly between events, but not when the finger is dragged ontop outside of the element
                } else if (event.getAction() == MotionEvent.ACTION_OUTSIDE){
                    //Can't get this to trigger in any case but I think it might be the solution
                } else if (event.getAction() == MotionEvent.ACTION_UP){
                    //Triggers correctly
                }
                }
                return false;
            }
        });
tjb
  • 11,480
  • 9
  • 70
  • 91

2 Answers2

1

ACTION_OUTSIDE can be triggered only when your touch area is extended by the ViewRoot.

If you think about dragging, you have 2 options, but for both of them you can detect rectangle, where your button positioned on screen View.getHitRect(Rect) and then, basing on this knowledge you can decide wheter MotionEvent is inside Rect or not.

Otherwise you can create DragLayer and implement all logic there.

acm0x
  • 775
  • 6
  • 14
  • thanks, I'll give it a try. I don't understand what you mean by "extended by the ViewRoot", are you trying to say that ACTION_OUTSIDE is something Android uses internally and a user of the framework shouldn't use? – tjb May 28 '11 at 15:42
  • 1
    there's a method, which allow your View has larger Touch area than real size. Actually that event is not meaning that you moving outside your hit rect. – acm0x May 28 '11 at 15:45
  • OK, sort of like moving in a "margin" of your view I guess. – tjb May 28 '11 at 15:47
0

You might want to give the Gesture Listener a try. There is a rather good description about how to use it here.

It seems like you need to extend Button and add possibilities for gesture detection in order to accomplish this.

Community
  • 1
  • 1
Ben Weiss
  • 17,182
  • 6
  • 67
  • 87