1

I have a custom view that can be dragged around the screen and when tapped it will display another view, everything works fine when it is used in the emulator. But when on a real device the custom view does not show the new view on click. And I am struggling to understand why.

I think this is where the problem is: the other view only shows on tap when I put the view on the leftmost side of the screen, but if I change the sign > to < now the view can be opened from the rightmost side but not on the left.

 case MotionEvent.ACTION_MOVE:
                    if(motionEvent.getRawX() > 120 && motionEvent.getRawY() > 120){
                        params.x = initialX + (int) (motionEvent.getRawX() - initialTouchX);
                        params.y = initialY + (int) (motionEvent.getRawY() - initialTouchY);
                        mWindowManager.updateViewLayout(entireLayout, params);

                        moving = true;
                    }

This is the complete code:

  collapsedLayout.setOnTouchListener(new View.OnTouchListener() {
        private int initialX;
        private int initialY;
        private float initialTouchX;
        private float initialTouchY;
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch (motionEvent.getAction()){
                case MotionEvent.ACTION_DOWN:
                    initialX = params.x;
                    initialY = params.y;
                    initialTouchX = motionEvent.getRawX();
                    initialTouchY = motionEvent.getRawY();

                    return true;

                case MotionEvent.ACTION_MOVE:
                    if(motionEvent.getRawX() > 120 && motionEvent.getRawY() > 120){
                        params.x = initialX + (int) (motionEvent.getRawX() - initialTouchX);
                        params.y = initialY + (int) (motionEvent.getRawY() - initialTouchY);
                        mWindowManager.updateViewLayout(entireLayout, params);

                        moving = true;
                    }
                    return true;

                case MotionEvent.ACTION_UP:

                    if(moving){
                        if (params.x > 0) {
                            params.x = (int) dpWidth;
                        } else if (params.x < 0) {
                            params.x = (int) dpWidth * -1;
                        }
                        mWindowManager.updateViewLayout(entireLayout, params);
                        moving = false;
                    }else {
                        collapsedLayout.setVisibility(View.GONE);
                        expandedLayout.setVisibility(View.VISIBLE);
                    }
                    return true;
            }
            return false;
        }
    });

Thanks in advance for any help!

Gionne Lapuz
  • 518
  • 1
  • 9
  • 23
  • 1
    when you "tap" it might still be doing an ACTION_MOVE event which is setting your `moving` to true. Consider using an click listener instead of a touch listener for the tap, or add a time frame for the up event (e.g. if the up event is within 1 second of the down, then its a click) – RobVoisey Nov 27 '17 at 14:18
  • 1
    I would recommend also adding some logs to see what events are callled and when. – jorjSB Nov 27 '17 at 14:26
  • @RobVoisey Yes sir, you are right, I went for implementing an onTouch for dragging and an onclick listener for showing the view. I tried it a while ago by myself and it didn't work, so I went to check for a method how to achieve that when you said it was possible. And poof it finally worked! haha. Thanks a lot sir! – Gionne Lapuz Nov 27 '17 at 14:27
  • @GionneLapuz glad it worked! – RobVoisey Nov 27 '17 at 14:29
  • @jorjSB I would take that in mind the next time I post a problem regarding events to better understand my problem, thanks for the advice sir. – Gionne Lapuz Nov 27 '17 at 14:36

1 Answers1

0

I found the method on how to do thanks to RobVoisey. I implemented a Ontouch listener for the Dragging of view and an On click listener for showing the view when tapped. I used this solution how to use both Ontouch and Onclick for an ImageButton?

and for the implementation it is like this for anyone who wants to see the answer:

   private GestureDetector gestureDetector;

   public void displayOverlay() {
   gestureDetector = new GestureDetector(this, new SingleTapConfirm());

   @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (gestureDetector.onTouchEvent(event)) {

                return true;
            } else {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        initialX = params.x;
                        initialY = params.y;
                        initialTouchX = event.getRawX();
                        initialTouchY = event.getRawY();
                        return true;

                    case MotionEvent.ACTION_UP:
                        if (params.x > 0) {
                            params.x = (int) dpWidth;
                        } else if (params.x < 0) {
                            params.x = (int) dpWidth * -1;
                        }
                        mWindowManager.updateViewLayout(entireLayout, params);
                        return true;

                    case MotionEvent.ACTION_MOVE:
                        params.x = initialX + (int) (event.getRawX() - initialTouchX);
                        params.y = initialY + (int) (event.getRawY() - initialTouchY);
                        mWindowManager.updateViewLayout(entireLayout, params);
                        return true;
                }
            }
            return false;
        }
    });
   }


 private class SingleTapConfirm extends GestureDetector.SimpleOnGestureListener {
    @Override
    public boolean onSingleTapUp(MotionEvent event) {
        collapsedLayout.setVisibility(View.GONE);
        expandedLayout.setVisibility(View.VISIBLE);

        return true;
    }
}
Gionne Lapuz
  • 518
  • 1
  • 9
  • 23