MotionEvent - How to do action on tap, not drag [duplicate]












0
















This question already has an answer here:




  • How can I detect a click in an onTouch listener?

    9 answers




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










share|improve this question













marked as duplicate by 0X0nosugar, Community Jan 1 at 17:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • 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 1 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)

    – 0X0nosugar
    Jan 1 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 1 at 17:17
















0
















This question already has an answer here:




  • How can I detect a click in an onTouch listener?

    9 answers




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










share|improve this question













marked as duplicate by 0X0nosugar, Community Jan 1 at 17:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • 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 1 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)

    – 0X0nosugar
    Jan 1 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 1 at 17:17














0












0








0









This question already has an answer here:




  • How can I detect a click in an onTouch listener?

    9 answers




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










share|improve this question















This question already has an answer here:




  • How can I detect a click in an onTouch listener?

    9 answers




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





This question already has an answer here:




  • How can I detect a click in an onTouch listener?

    9 answers








android listview touch listitem motionevent






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 16:14









Ishaan JavaliIshaan Javali

1,3543820




1,3543820




marked as duplicate by 0X0nosugar, Community Jan 1 at 17:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by 0X0nosugar, Community Jan 1 at 17:18


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • 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 1 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)

    – 0X0nosugar
    Jan 1 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 1 at 17:17



















  • 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 1 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)

    – 0X0nosugar
    Jan 1 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 1 at 17:17

















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 1 at 16:56





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 1 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)

– 0X0nosugar
Jan 1 at 17:02





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)

– 0X0nosugar
Jan 1 at 17:02




1




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 1 at 17:17





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 1 at 17:17












2 Answers
2






active

oldest

votes


















0














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






share|improve this answer


























  • 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 1 at 16:24













  • ok i will see for this

    – Gourav
    Jan 1 at 16:25











  • See edited answer!

    – Gourav
    Jan 1 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 1 at 16:34











  • Tell me one thing, do you have listview in your activity?

    – Gourav
    Jan 1 at 16:36



















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






share|improve this answer






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    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






    share|improve this answer


























    • 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 1 at 16:24













    • ok i will see for this

      – Gourav
      Jan 1 at 16:25











    • See edited answer!

      – Gourav
      Jan 1 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 1 at 16:34











    • Tell me one thing, do you have listview in your activity?

      – Gourav
      Jan 1 at 16:36
















    0














    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






    share|improve this answer


























    • 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 1 at 16:24













    • ok i will see for this

      – Gourav
      Jan 1 at 16:25











    • See edited answer!

      – Gourav
      Jan 1 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 1 at 16:34











    • Tell me one thing, do you have listview in your activity?

      – Gourav
      Jan 1 at 16:36














    0












    0








    0







    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






    share|improve this answer















    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







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 1 at 16:31

























    answered Jan 1 at 16:23









    GouravGourav

    7501629




    7501629













    • 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 1 at 16:24













    • ok i will see for this

      – Gourav
      Jan 1 at 16:25











    • See edited answer!

      – Gourav
      Jan 1 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 1 at 16:34











    • Tell me one thing, do you have listview in your activity?

      – Gourav
      Jan 1 at 16:36



















    • 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 1 at 16:24













    • ok i will see for this

      – Gourav
      Jan 1 at 16:25











    • See edited answer!

      – Gourav
      Jan 1 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 1 at 16:34











    • Tell me one thing, do you have listview in your activity?

      – Gourav
      Jan 1 at 16:36

















    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 1 at 16:24







    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 1 at 16:24















    ok i will see for this

    – Gourav
    Jan 1 at 16:25





    ok i will see for this

    – Gourav
    Jan 1 at 16:25













    See edited answer!

    – Gourav
    Jan 1 at 16:31





    See edited answer!

    – Gourav
    Jan 1 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 1 at 16:34





    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 1 at 16:34













    Tell me one thing, do you have listview in your activity?

    – Gourav
    Jan 1 at 16:36





    Tell me one thing, do you have listview in your activity?

    – Gourav
    Jan 1 at 16:36













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






    share|improve this answer




























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






      share|improve this answer


























        0












        0








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






        share|improve this answer













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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 1 at 17:16









        Gurgen ArustamyanGurgen Arustamyan

        11




        11















            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith