0

When I press my finger on a button and then move it outside button's bounds, the onTouchListener continues firing.

The same thing happens when I press one finger on the button and a second finger outside the button: my onTouchListener fires for the second finger.

How do I can avoid this? I want to fire my onTouchListener only when I press inside a button's bounds.

Two fingers

One finger

Here is my onTouchListener implementation

View.OnTouchListener onTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if (fingerDown) {
            xHistoricalFirstFinger = (int) event.getX();
            yHistoricalFirstFinger = (int) event.getY();
            xFirstClick = xHistoricalFirstFinger;
            yFirstClick = yHistoricalFirstFinger;
            //counter++;
            fingerDown = false;
        }
        x = (int) event.getX();
        y = (int) event.getY();

        Log.v("Touched", client.remoteAdress.toString());

        mouseMoveProvider.nextX = x - xHistoricalFirstFinger;
        mouseMoveProvider.nextY = y - yHistoricalFirstFinger;
        mouseMoveProvider.Ready = true;
        xHistoricalFirstFinger = x;
        yHistoricalFirstFinger = y;

        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (x - xFirstClick == 0 & y - yFirstClick == 0) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            client.MakeMove();
                        } catch (IOException e) {
                            e.printStackTrace();
                            NetworkListener.HandleNetworkIsUreachableException(context);
                        }
                    }
                }).start();
            }

            fingerDown = true;

        }
        return true;
    }
};
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Kotelnikov
  • 11
  • 2

2 Answers2

1

I think this might be of help to you:

User touch and drags out of button region

Basically you also need to check for ACTION_MOVE and see if the touch has left the bounds of your rectangle. I hope that helps.

Community
  • 1
  • 1
Sam Calmes
  • 36
  • 1
0

Use onClickListener?

onTouchListener will fire for every touch (finger down, finger up...)

JoKr
  • 4,976
  • 8
  • 27
  • 39
  • I need only touch listener, that firing only inside button's bounds – Kotelnikov Jul 30 '16 at 15:31
  • I think you could check if the touch event coordinates are inside the view. Haven't done this, but looking only at documentation, you can do `v.getDrawingRect().contains(event.getX(), event.getY())` which will check if touch event coordinates are inside view rectangle - returns boolean. – JoKr Jul 30 '16 at 15:41
  • I've been thinking about it, but your method is kind of workaround, isn't it? Does more elegant solution exist? – Kotelnikov Jul 30 '16 at 15:48
  • I don't think this is workaround, that's how `onTouchListener` works and one line of code is pretty elegant. Only "more elegant" solution I could think of is `onClickListener` :D – JoKr Jul 30 '16 at 16:14