Search code examples
androidontouchlistenerontouch

Ontouch Listener on Custom View


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!


Solution

  • 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;
        }
    }