2

I have some code that has got me very close to what Im trying to achieve. But there are a few issues. What I want to do is, when a Button is pressed and held, it will continuously call a method AdvanceLog(). The problem is, the ACTION_DOWN motion event does not continue to get called. What happens is, when you press and hold, the first motion is ACTION_DOWN and every motion thereafter while still holding down is ACTION_MOVE. This is causing some issues, as I am not REALLY looking for the move action, more so just the repetetive down action. Any suggestions?

advanceButton.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) 
            {
                int counter=0;
                if(event.getAction()==MotionEvent.ACTION_DOWN || event.getAction()==MotionEvent.ACTION_MOVE)
                {
                    fwdTouchCounter++;
                    AdvanceLog(true, true);
                    try 
                    {
                        if(fwdTouchCounter>5)                           
                            Thread.sleep(30);
                        else
                            Thread.sleep(800);
                    } 
                    catch (InterruptedException e) 
                    {

                        e.printStackTrace();
                    }
                }
                else if(event.getAction()==MotionEvent.ACTION_UP)
                    fwdTouchCounter=0;

                return false;

            }
        });
Jesse
  • 2,674
  • 6
  • 30
  • 47

1 Answers1

4

On your ACTION_DOWN touch event, fire off a thread that calls a local method. Inside this method constantly read a class variable (boolean is easiest) and if that variable, let's call it m_advanceLog is true, continue to process (advancing your log I'm guessing). In your ACTION_UP touch event, set m_advanceLog to false which should stop your loop and allow the thread to exit.

Keep in mind that this is only a good idea if there aren't user interface changes, othwerwise some additional work is needed due to the thread interaction.

Matt Bishop
  • 1,010
  • 7
  • 18
  • @MattBishop hi, I am having a problem quite similar to the above mentioned. Instead of detecting my finger that is touching the screen and stationary, it is constantly firing Action_Move! Please take a look at my code and I'd appreciate your input. http://stackoverflow.com/questions/28074817/surfaceview-multi-touch-motionevent-action-move-gets-executed-wrongly-and-conti – user3833732 Feb 08 '15 at 13:53