1

I have been searching online and on StackOverflow for how to do an action in an onTouchListener if the event was a tap, not a motion of any kind, but I haven't been able to find an answer and what I have tried doesn't work either. I have a ListView Adapter and in the getView method, I have set the onTouchListener on the listItem.

What I want to do is when the listItem is tapped, I want to display a Toast, but if the event was a drag (like the user scrolling through the list), I don't want to display the Toast.

Below is the code that I have tried. (makeToast is a function for making a Toast and events.getRelation() is the String I want to have in the Toast.):

listItem.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
            makeToast(events.getRelation());
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            //  makeToast(events.getRelation());
        } else if (event.getAction() == MotionEvent.ACTION_SCROLL) {
            //   makeToast(events.getRelation());
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            // makeToast(events.getRelation());
        } else if (event.getAction() == MotionEvent.AXIS_SCROLL) {
            //   makeToast(events.getRelation());
        } else if (event.getAction() == MotionEvent.AXIS_VSCROLL) {
            //    makeToast(events.getRelation());
        } else {
            //makeToast(events.getRelation());
        }
        return false;
    }
});

I have tried having the makeToast method under different types of MotionEvents, but I still don't get the results I want because either a Toast is not made at all, or it is made when I am scrolling, not tapping. Would anyone be able to help me with this? I feel like it is a simple problem, but I have been unable to find a solution

Ishaan Javali
  • 1,711
  • 3
  • 13
  • 23
  • That question had to do with implementing `onTouch` and `onClick` because `onClick` is never called after `onTouch`. My question is differentiating between a scroll and a tap to create the `Toast` on the tap. – Ishaan Javali Jan 01 '19 at 16:56
  • I agree that the other user's *reason* for wanting to be able to detect a tap/ a click in `onTouch()` is different from yours. But since both of you want to *achieve* the same thing, please explain why the answers to the other post are not helpful in your case. (IMO the accepted self-answer is not really good but there is more than one other answer explaining how to evaluate the time between DOWN and UP in order to make a decision) – Bö macht Blau Jan 01 '19 at 17:02
  • 1
    Thanks 0X0nosugar. The second answer from that post helped me. I was not able to find an answer before you posted that so thanks. – Ishaan Javali Jan 01 '19 at 17:17

2 Answers2

0

To detect Single and Double Tap in android use the following methods 

class GestureTap extends GestureDetector.SimpleOnGestureListener { @Override public boolean onDoubleTap(MotionEvent e) { Log.i("onDoubleTap :", "" + e.getAction()); return true; } @Override public boolean onSingleTapConfirmed(MotionEvent e) { Log.i("onSingleTap :", "" + e.getAction()); return true; } }

And add the following code in onTouch listener

@Override public boolean onTouchEvent(MotionEvent event) { new GestureDetector(this, new GestureTap()).onTouchEvent(event); return true; }

-1

According to what I understand, I think you wish to set Click listener to the whole activity!
To do this, add ViewTreeObserver:

ViewTreeObserver viewTreeObserver = myView.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    //make Toast
}

Source: https://stackoverflow.com/a/18000128/9819031

Gourav
  • 2,746
  • 5
  • 28
  • 45
  • I don't think you understood my question. I know how to make a `Toast`. What I am having difficulty with is detecting whether a touch was a tap or a motion and then making a `Toast` if it was a tap. The problem is with detecting a touch vs motion. – Ishaan Javali Jan 01 '19 at 16:24
  • ok i will see for this – Gourav Jan 01 '19 at 16:25
  • See edited answer! – Gourav Jan 01 '19 at 16:31
  • Thanks for trying Gourav, but I don't want to set a `ClickListener` to the whole activity because each individual row in the `ListView` makes a `Toast` of a different message. Also, my question is about detecting different types of `MotionEvents` like taps or drags, not making `Toasts` or `ClickListeners`. – Ishaan Javali Jan 01 '19 at 16:34
  • Tell me one thing, do you have listview in your activity? – Gourav Jan 01 '19 at 16:36
  • Yes I have a Listview. That is why, as I said in the question, I'm implementing the `getView` method. – Ishaan Javali Jan 01 '19 at 16:38
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/186014/discussion-between-gourav-and-ishaan-javali). – Gourav Jan 01 '19 at 16:38