Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make press and hold button to Start a timer?

I want to make a android timer app. The timer starts when we press a button and hold it and stops when releasing a button. Please tell me the method that functions the same. I am following an example http://examples.javacodegeeks.com/android/core/os/handler/android-timer-example/ to make a timer.

like image 827
theone1one Avatar asked Jun 27 '15 13:06

theone1one


2 Answers

override onTouch listener to the button with your timer logic

findViewById(R.id.btn_add).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    // start your timer

                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                   // stop your timer.

                }
                return false;
            }
        });
like image 193
Krishna V Avatar answered Sep 20 '22 00:09

Krishna V


I think this could help you:

       button.setOnTouchListener(new View.OnTouchListener() {
       @Override
       public boolean onTouch(View v, MotionEvent event) {
           switch (event.getAction()) {
               case MotionEvent.ACTION_DOWN: {
                   // Button is pressed
                   break;
               }
               case MotionEvent.ACTION_UP: {
                   // Button is not pressed
               }
           }
           return true;
       }
    });
like image 21
ruclip Avatar answered Sep 22 '22 00:09

ruclip