1

I'm overriding some functions of the GestureDetector, and I detect a long press and would like to also detect once it's released, but I haven't found the function for. Actually the only function I found for releasing the tap is onSingleTapUp. Is it possible to detect when a long press is released?

My code:

class GestureListener (val position: Int) : GestureDetector.SimpleOnGestureListener() {

        override fun onLongPress(e: MotionEvent?) {
            // Do something here
            super.onLongPress(e)
        }
vatbub
  • 2,713
  • 18
  • 41
sir-haver
  • 3,096
  • 7
  • 41
  • 85
  • Check this https://stackoverflow.com/a/25487744/9030938 Hope it will help – Naveen Dec 25 '19 at 12:28
  • I did return true in the function onDown, it still doesn't help, I suspect that it's just impossible unless you edit the GestureDetector itself, thanks – sir-haver Dec 25 '19 at 12:45

2 Answers2

1

In Java You Can Do Like This

 boolean isDown = false;

and

 textView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int action = event.getAction();
                if (action == MotionEvent.ACTION_DOWN) {
                    Toast.makeText(v.getContext(), "down: " , 1000).show();
                    isDown = true;
                }

                else if (action == MotionEvent.ACTION_UP) {


                    if (isDown) {
                        // touch press complete, show toast
                        Toast.makeText(v.getContext(), "User Release Long Press: " , 1000).show();
                        isDown = false;

                    }
                }


                return true;
            }
        });
  • Thank you for your effort, I would like to still try and implement it in the GestureDetector if it's possible – sir-haver Dec 25 '19 at 12:45
1

Apparantly you have to work with GestureDetector in conjunction with OnTouchEvent:

val detector = GestureDetector(object: GestureDetector.SimpleOnGestureListener() {
            override fun onScroll(
                e1: MotionEvent?,
                e2: MotionEvent?,
                distanceX: Float,
                distanceY: Float
            ): Boolean {
                currentAction = "isScrolling"
                Log.d("TAG", "SCROLLING")
                return true
            }

            override fun onLongPress(e: MotionEvent?) {
                Log.d("TAG", "Long press!")
                currentAction = "isLongPressed"
                super.onLongPress(e)
            }

            override fun onDown(e: MotionEvent?): Boolean {
                return true
            }
        })

        val gestureListener = View.OnTouchListener(function = { view, event ->
            detector.onTouchEvent(event)

            if(event.getAction() == MotionEvent.ACTION_UP) {
                when (currentAction) {
                    "isScrolling" -> {
                        Log.d("TAG", "Done scrolling")
                        currentAction = ""
                    }
                    "isLongPressed" -> {
                        Log.d("TAG", "Done long pressing")
                        currentAction = ""
                    }
                }

            }

            false
        })

        profilePic1.setOnTouchListener(gestureListener)

The ACTION_UP is available and can detect any release of the finger, whether from longPress, scrolling, and so on

sir-haver
  • 3,096
  • 7
  • 41
  • 85