0

I apologise for the somewhat noobish question. I've tried googling it, searching this site and the android developer site, yet didn't manage to find an answer.

I'm looking for a listener that differentiates between up and down events. Currently I'm using OnTouchListener, but it gets triggered very fast, causing a noticable lag, even when I immediately return after an event that is not Down and Up.

I've also tried OnClickListener, but it seems to only get triggered when you lift your finger up, rather than have seperate events for down click and up click like I need.

Would appreciate some help,

Thanks!

Ungoliant
  • 133
  • 2
  • 14
  • 1
    "but it gets triggered very fast, causing a noticable lag," -- Without context, this sentence doesn't make sense and doesn't explain why and `OnTouchListener` wouldn't suit your needs. – Karakuri Aug 08 '16 at 20:20
  • On what are you using that event ? lag on what ? Scrolling ? Or the speed of the event dispatch ? – Lucas Queiroz Ribeiro Aug 08 '16 at 20:24
  • Thanks for the replies. I shall try to explain. I'm making a sort of custom UI, using a clickable image, like whats described here on the first part of the first answer: http://stackoverflow.com/questions/16670774/clickable-area-of-image My image of the UI is the background for a transparent image thats covering it, where each button is covered by an invisible unique color, which enables me to differentiate between the "buttons" (just drawings on the picture) by checking each time the image is clicked, which color is in the pixel. The lag I'm talking about is just noticable graphic lag. – Ungoliant Aug 08 '16 at 20:33

3 Answers3

1

See this:

https://developer.android.com/training/graphics/opengl/touch.html

Override the following method on your activity, then you can treat each case, when the user touch the screen, and when he take of his finger.

 @Override
 public boolean onTouchEvent(MotionEvent e) {
    // MotionEvent reports input details from the touch screen
    // and other input controls. In this case, you are only
    // interested in events where the touch position changed.

    float x = e.getX();
    float y = e.getY();

    switch (e.getAction()) {
        case MotionEvent.ACTION_DOWN:
          // do your stuff
          break;
        case MotionEvent.ACTION_UP:
          // do your stuff
          break;
    }

    return true;
}
Riten
  • 2,879
  • 3
  • 24
  • 28
Felipe R. Saruhashi
  • 1,709
  • 16
  • 22
1
 YourBtn.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event){
                if(event.getAction() == MotionEvent.ACTION_UP){
                    //do stuff
                }
                else if(event.getAction() == MotionEvent.ACTION_DOWN){
                    //do stuff
                }
                else if(event.getAction() == MotionEvent.ACTION_CANCEL){
                    //do stuff
                }
                return true; //return false can be used it depends
            }
        });

P.S: it can be: a button, a textview, an imageview or any UI element

I strongly recommend to always add ACTION_CANCEL to avoid any misbehaviours or crashes

Thorvald
  • 3,424
  • 6
  • 40
  • 66
0

You should look at GestureDetector to simplify your touch event handling.

kris larson
  • 30,387
  • 5
  • 62
  • 74