Search code examples
androidonclickandroid-windowmanagerandroid-eventontouch

OnClickListener and OnTouchListener collision issues


Currently working on an app in which I implement a chat head to make a view on top. I use an image for floating icon for chat heads, it working fine. what problem I face is that, both onClick and onTouch event not working properly either one of them is working at a time. How to make both of them working?.


Solution

  • I'd suggest you use only the OnTouchListener and handling the click case there using something like:

        ImageButton floatingIcon = (ImageButton) findViewById(R.id.floatingIcon);
        gestureDetector = new GestureDetector(this, new YourGestureListener());
        floatingIcon.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                if (gestureDetector.onTouchEvent(arg1)) {
                    // A single tap has been made - treat it like a click and consume the event
                    return true;
                } else {
                    // The MotionEvent is not a click event, handle and decide if the event is consumed
                }
                return false;
            }
        });
    
        private class YourGestureListener extends SimpleOnGestureListener {
            @Override
            public boolean onSingleTapUp(MotionEvent event) {
                return true;
            }
        }