0

I have a EditText. I've changed its default behaviour so I need to handle onTouchListener and OnClickListener at the same time.

The default system uses onTouchListener for change the cursor position, so I captured it with this code:

var otl = OnTouchListener { v, event ->
  when (event.action) {
    MotionEvent.ACTION_DOWN -> {
      editX = event.x  
      editY = event.y
      Log.d("myMess","touch click")  // debug in logcat
    }
  }
  false 
}
...
ed.setOnTouchListener(otl)

Notice that I return false, so the click event should be fired after.

I've read all stuff in Stackoverflow including this

I just put the logcat displays after I've noticed the problem, so there is no influence for the time interval. I use elapsedRealtime for measuring the intervals and printing in my logcat.

In another point I set:

ed.setOnClickListener(::clickS)

fun clicks(vi:View:){
    if (vi==ed) {
        Log.d("myMess","simple click")  // debug in logcat
        ...
    }
}

Until around 300ms, OnClickListener don't fire, above this time it fires normally.

It's a big mistery. I have no hint for my problem.

Paulo Buchsbaum
  • 2,471
  • 26
  • 29
  • 1
    in your onTouch Listener, override onclick listener and call it inside onTouchListener method. – RajeshVijayakumar Aug 09 '19 at 05:18
  • 1
    Did you see this question https://stackoverflow.com/questions/5040303/how-can-i-make-a-button-more-responsive/5041276 ? – Andrei Tanana Aug 09 '19 at 05:43
  • 1
    just try it without event.action -> MotionEvent.ACTION_DOWN. write your code without when condition. just try it once. – Shweta Chauhan Aug 09 '19 at 06:07
  • 1
    if youre using listeners on the same object its becoming a bit tricky. How about pretend the onclick in touch listener ? Like if `event.x` is the same when `ACTION_DOWN` and `ACTION_UP` or in some small radius like 10 px then its a click – P.Juni Aug 09 '19 at 09:27
  • [Shweta Chauhan](https://stackoverflow.com/users/6021469/shweta-chauhan), I need ACTION_DOWN to record the cursor position. – Paulo Buchsbaum Aug 09 '19 at 14:04
  • [RajeshVijayakumar](https://stackoverflow.com/users/1369752/rajeshvijayakumar), I've some tries calling onclick listener in onTouch. I will post what has solved my problem. – Paulo Buchsbaum Aug 09 '19 at 14:06
  • [Andrei Tanana](https://stackoverflow.com/users/3350224/andrei-tanana). `SetPressed` has not worked for me. – Paulo Buchsbaum Aug 09 '19 at 14:06
  • [P.Juni](https://stackoverflow.com/users/5909412/p-juni), something in that direction has worked for me. I will post it. – Paulo Buchsbaum Aug 09 '19 at 14:07

1 Answers1

0

I follow the P.Juni suggestion and it work like a charm.

This is my new code:

var otl  = OnTouchListener { v, event ->
  when (event.action) {
    MotionEvent.ACTION_DOWN -> {
      editX = event.x
      editY = event.y
    }
    MotionEvent.ACTION_UP -> {  
      if ( abs(event.x - editX) <=15.0 &&  abs(event.x - editX) <=15.0  )
        clickS(v)

    }
  }
  false
}
...
ed.setOnTouchListener(otl)

Click handling code is the same. No changes

fun clicks(vi:View:){
    if (vi==ed) {
        ...
    }
}

a) I've used 15 pixel margin for detect a real click (ACTION_UP and ACTION_DOWN) almost in the same screen place.
b) I haven't setted a click event anymore for buton ed.
c) I call directly clickS in touch handling code.
d) I keep on returning false in touch event handling

It is a good workaround, but it doesn't explain the problem. My previous code should work!

Not it works even with 150 ms time interval.

Paulo Buchsbaum
  • 2,471
  • 26
  • 29
  • 1
    why dont you use [GestureDetector](https://developer.android.com/training/gestures/detector#detect) instead? – pskink Aug 10 '19 at 02:57
  • I think that it's not necessary go so far in that phase of my app, because I'm not using pinches, drag and drop or multitouch. My solution now already evolves: It does everything using just `TouchEvent`. I am not using `onClickListening` or `onLongClickListening` anymore. Vide [stackoverflow](https://stackoverflow.com/questions/57438401/why-ontouchevent-is-not-always-fired-when-cursor-is-on). It works very smoothy – Paulo Buchsbaum Aug 10 '19 at 03:27
  • you have `onSingleTapUp` for that (or `onSingleTapConfirmed`) - all you have to do is to extend [SimpleOnGestureListener](https://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener) for example – pskink Aug 10 '19 at 03:28
  • I swear that I've started to do this way, but I didn't like the inflexibility of "simple gestures". Using my heterodox solution, I can choice each time lapse and location sensitivity. I think that, in my case, it suits me better. – Paulo Buchsbaum Aug 10 '19 at 03:37