0

I have an app with two different views, each view haves its own onTouchListener and each ontouchlistener does different things. The two views listeners works if i touch them alone. The problem is that if one view is pressed and capturing the touch event, the other view can't be preseed, they does not work in mutltitouch! I want that the two ontouchlisteners of the two views works at same time. How can this be achieved?

this is the code for the VIEW A:

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();
        switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_MOVE: {
                if (disabled==true)
                    break;
                return processMoveEvent(ev);
            }       
            case MotionEvent.ACTION_CANCEL: 
            case MotionEvent.ACTION_UP: {
                if ( pointerId != INVALID_POINTER ) {
                    returnHandleToCenter();
                    returnHandleToCenterDelayedMovement();
                    setPointerId(INVALID_POINTER);
                }
                break;
            }
            case MotionEvent.ACTION_POINTER_UP: {
                if ( pointerId != INVALID_POINTER ) {
                    final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
                    final int pointerId = ev.getPointerId(pointerIndex);
                    if ( pointerId == this.pointerId ) {
                        returnHandleToCenter();
                        returnHandleToCenterDelayedMovement();
                        setPointerId(INVALID_POINTER);
                        return true;
                    }
                }
                break;
            }
            case MotionEvent.ACTION_DOWN: {
                if ( pointerId == INVALID_POINTER ) {
                    int x = (int) ev.getX();
                    if ( x >= offsetX && x < offsetX + dimX ) {
                        setPointerId(ev.getPointerId(0));
                        if (disabled==true){
                            return true;
                        }                       
                        return processMoveEvent(ev);
                    }
                }
                break;
            }
            case MotionEvent.ACTION_POINTER_DOWN: {
                if ( pointerId == INVALID_POINTER ) {
                    final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
                    final int pointerId = ev.getPointerId(pointerIndex);
                    int x = (int) ev.getX(pointerId);
                    if ( x >= offsetX && x < offsetX + dimX ) {
                        setPointerId(pointerId);
                        return true;
                    }
                }
                break;
            }
        }
        return false;
    }

and this is the code for the VIEW B:

    viewB.setOnTouchListener(new OnTouchListener() {            
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            touchX=event.getX();
            touchY=event.getY();                

            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                TestManager.getInstance().startThread();
                break;
            case MotionEvent.ACTION_UP:
                TestManager.getInstance().stopThread();
                break;            
            }

            return true;
        }
    });
NullPointerException
  • 36,107
  • 79
  • 222
  • 382
  • Joystick still isn't working, then? – vipluv Feb 05 '15 at 11:48
  • what? the two views work if i touch them alone, The problem is that if one view is pressed and capturing the touch event, the other view can't be preseed, they does not work in mutltitouch! I want that the two ontouchlisteners of the two views works at same time. How can this be achieved? – NullPointerException Feb 05 '15 at 11:48
  • You asked this question before as well, remember? i had said to add POINTER_DOWN and UP events to the weapon's ontouchListener as well. That didn't work, and now i'm out of ideas too. – vipluv Feb 05 '15 at 11:49
  • and it does not work, i told you in that question, i hope someone knows how to make this work! it is very simple, two views capturing events in their two listeners... it is crazy that android does not provide a easy way to achieve this! – NullPointerException Feb 05 '15 at 11:50
  • you need to keep the ACTION_UP and DOWN events as well, and *add* the new POINTER events separately. Did you do that? – vipluv Feb 05 '15 at 11:51
  • one view? it wont work for this app, it is a very huge app with tons of views and tons of functionalities, this must be achieved by other way – NullPointerException Feb 05 '15 at 11:52
  • trust me, that approach won't fit in this app. Must be achieved by other way – NullPointerException Feb 05 '15 at 11:58
  • What sdk are you testing on? – pskink Feb 05 '15 at 12:07
  • im not testing any sdk, it is a custom app – NullPointerException Feb 05 '15 at 12:13
  • it is not possible in android to have two views with their own touchlistener working at same time? – NullPointerException Feb 05 '15 at 12:14
  • what android sdk level are you testing on? – pskink Feb 05 '15 at 12:19
  • api 13 level sdk. it is not possible in android to have two views with their own touchlistener working at same time? – NullPointerException Feb 05 '15 at 12:34
  • have you called setMotionEventSplittingEnabled(boolean) ? what does isMotionEventSplittingEnabled() return ? – pskink Feb 05 '15 at 12:40
  • it is enabled on view B but not available in view A because view A extends from View and for some reason does not implement that method (isMotionEventSppliting) – NullPointerException Feb 05 '15 at 12:57
  • did you call isMotionEventSplittingEnabled anyway? – pskink Feb 05 '15 at 13:01
  • yes it is enabled on view B (viewB is a layout) but not available in view A because view A extends from View and for some reason does not implement that method (isMotionEventSppliting) – NullPointerException Feb 05 '15 at 13:02
  • Override the parent's dispatchTouchEnent and see where the events are dispatched – pskink Feb 05 '15 at 14:10
  • the events are captured by the first view i touch, A or B, and then all the events are captured by that view until i release all the fingers in the screen.... – NullPointerException Feb 05 '15 at 14:50
  • so the MotionEvents rhat are generated outside the first view are dispatchet to that view too? try to run for example std calculator app press and HOLD button 0 and then press button 9 and see what happens – pskink Feb 05 '15 at 15:18
  • it works perfectly in the calculator, also it is not exactly the same case. Here i need to make different actions in different views with different ontouchlisteners.... it is possible to achieve this or it is imposible? – NullPointerException Feb 05 '15 at 15:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70316/discussion-between-androiduser99-and-pskink). – NullPointerException Feb 05 '15 at 15:40

0 Answers0