0

I would like to to have a button that executes a method repeatedly when it is pressed. Once it is released it should stop executing the method.

Example: A user presses and holds down the button for 5 seconds. Then the method shall be executed over and over again during those 5 seconds. In addition, the method that gets called repeatedly needs to know for how long the button is pressed so far.

Here is what I have so far. I am using an onTouchListener:

myButton.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                myButton.setText("Pressed!");
                while(myButton.isPressed()) {
                    doSomething(event);
                }
                break;  

            case MotionEvent.ACTION_UP:
                myButton.setText("Press me!");
            }
            return false;
        };

        private void doSomething(MotionEvent event) {
            // Do some calculation with the time the button is pressed so far
        }
    });

My problem is that the while-loop is not working as it should and that I dont know how to get the time the button was pressed. Maybe with the event?? Any ideas?

user2426316
  • 7,131
  • 20
  • 52
  • 83

3 Answers3

1

Use a Runnable and a Handler to run your method periodically.-

private final int INTERVAL = 100;
private Handler handler = new Handler();
private Runnable autoRotateRunnable =  new Runnable() { 
    @Override
    public void run() {
        periodicMethod();
        schedulePeriodicMethod();
    }
};

public void schedulePeriodicMethod() {
    handler.postDelayed(runnable, INTERVAL);
}

public void stopPeriodicMethod() {
    handler.removeCallbacks(runnable);
}

Your switch method would look like this.-

switch(event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        myButton.setText("Pressed!");
        schedulePeriodicMethod();
    break;  
    case MotionEvent.ACTION_UP:
        myButton.setText("Press me!");
        stopPeriodicMethod();
    }
}

As for the time between press down and up, you can store the current Date on ACTION_DOWN, and compare it with the Date on ACTION_UP.

ssantos
  • 16,001
  • 7
  • 50
  • 70
1

try this

myButton.setOnTouchListener(new View.OnTouchListener() {

      public boolean onTouch(View v, MotionEvent event) {

          if(event.getAction()== MotionEvent.ACTION_MOVE)
          {
              myButton.setText("Pressed!");

                  doSomething(event);
                  return true;   
          }
          if(event.getAction()== MotionEvent.ACTION_UP)
          {
              myButton.setText("Press me!");
              return false;
          }
          return true;
      }

      private void doSomething(MotionEvent event) {
          // Do some calculation with the time the button is pressed so far
      }
  });
vaibhav kumar
  • 414
  • 2
  • 11
0

On your button touch listener

Handler m_handler;
Runnable m_handlerTask ;  
m_handler = new Handler();   
m_handlerTask = new Runnable()
{
  @Override 
  public void run() { 

    // do something  
    m_handler.postDelayed(m_handlerTask, 1000);  // 1 second repeat  

  }
  };
 m_handlerTask.run();

To stop the run

 m_handler.removeCallbacks(m_handlerTask);

You can also use a timer. But timer runs on a different thread so use runOnUiThread to update ui if you use a timer. You can also use a count down timer.

Also check this

Android Thread for a timer

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256