I want to detect time press duration of button by using setOnTouchListener
with onTouch
method
How I can do this ?
I want to detect time press duration of button by using setOnTouchListener
with onTouch
method
How I can do this ?
you can do it using onTouchListener
long FirstTouchTime ;
long duration ;
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
FirstTouchTime = System.currentTimeMillis();
else if (event.getAction() == MotionEvent.ACTION_UP) {
duration = System.currentTimeMillis(); - FirstTouchTime ;
//duration value in millisecond
}
}
Maybe something like this :
long mLastClickTime;
findViewById(R.id.button).setOnTouchListener(new OnTouchListener() {
@Override
public void onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN)
mLastClickTime = SystemClock.elapsedRealtime();
else if (event.getAction() == MotionEvent.ACTION_UP) {
long duration = SystemClock.elapsedRealtime() - mLastClickTime;
// using threshold of 1000 ms
if (duration > 1000) {
// do your magic here
}
}
}
});