fisk
fisk

Reputation: 13

How to detect how long a button was pressed in Android?

simple question here:

I am working on a simple game, which the user need to press a button as long as sound is playing. Therefore i need to count for how long does the user press a button and compare it with the duration of the sound. The button does not fire an intent or do anything else.

thanks

Upvotes: 1

Views: 977

Answers (5)

Christophy Barth
Christophy Barth

Reputation: 487

In case you override onTouch(View v, MotionEvent event) and you get the warning message about

Custom view `View` has setOnTouchListener called on it but does not override performClick

You can use the SuppressLint below to stop it.

@SuppressLint("ClickableViewAccessibility")

However! Before you rush into using that, watch the video below to understand why the warning is shown https://www.youtube.com/watch?v=1by5J7c5Vz4

Upvotes: 0

fisk
fisk

Reputation: 13

Thanks you all....

I combined both AITALI and Pereira suggestions.. My code therefore is now this:

button.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {


            if (event.getAction() == MotionEvent.ACTION_DOWN){
                //USER START PRESSING THE BUTTON
                 startTime = SystemClock.elapsedRealtime();
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                //USER STOP PRESSING THE BUTTON
                timeInterval = SystemClock.elapsedRealtime() - startTime;
            }
            
            return true;
        }
    });

works perfectly

Upvotes: 0

D. Pereira
D. Pereira

Reputation: 116

You need to capture the time when the user presses down on the button, and the time when the user releases the button. The difference between these times is the duration of the press.

In order to achieve this, you will need to apply a View.OnTouchListener to the button, and use SystemClock.elapsedRealtime (as opposed to System.currentTimeMillis for this reason) to capture the time of the events.

button.setOnTouchListener (object : View.OnTouchListener {
  var startTime = 0L
  var endTime = 0L
  override fun onTouch(view: View?, motionEvent: MotionEvent?): Boolean {
    when (motionEvent?.action) {
      MotionEvent.ACTION_DOWN -> {
        // user pressed down on the button; capture start time
        startTime = SystemClock.elapsedRealtime()
        return true
      }
      MotionEvent.ACTION_CANCEL, // if user moves their finger off the button before releasing
      MotionEvent.ACTION_UP -> {
        // user released the button; capture end time and calculate the result
        endTime = SystemClock.elapsedRealtime()
        if (endTime > startTime) {
          val ellapsedTime = endTime - startTime
          // do something with ellapsedTime
        }
        return true
      }
    }
    return false
  }
})

Upvotes: 0

yourButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {

                    //USER START PRESSING THE BUTTON

                } else if (event.getAction() == MotionEvent.ACTION_UP) {

                    //USER STOP PRESSING THE BUTTON

                }
                return true;
            }
        });

Upvotes: 2

Nikola Despotoski
Nikola Despotoski

Reputation: 50578

You need to find delta time between putting the finger on and releasing

var now = 0L
var total = 0L
button.setOnTouchListener { v, event ->
  when (event.action) {
       ACTION_DOWN -> now = System.currentTimeMillis()
       ACTION_UP -> total = System.currentTimeMillis() - now
      }
   true
}

Upvotes: 0

Related Questions