Reputation: 3714
I want that when you are holding a button during 3 secs do something, I try with the code above but it only do semething when I stop holding not during the hold. Should I implement a listener or something like this?
finishPushing = true;
.
.
.
button.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event) {
if(android.os.SystemClock.elapsedRealtime() - event.getDownTime() > 3000 && (event.getPointerCount() == 1) && finishPushing)
{
// Do something
finishPushing = false;
}
else{
finishPushing = true;
}
return false;
}
});
Upvotes: 1
Views: 1273
Reputation: 3714
SOLVED thanks to Android Touch Event determining duration
button.setOnTouchListener(new OnTouchListener()
{
private final Handler handler = new Handler();
private final Runnable runnable = new Runnable() {
public void run() {
if(mBooleanIsPressed)
{
// do whatever
}
}
};
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
// Execute your Runnable after 5000 milliseconds = 5 seconds.
//After this 5secs it will check if is pressed
handler.postDelayed(runnable, 5000);
mBooleanIsPressed = true;
}
if(event.getAction() == MotionEvent.ACTION_UP) {
if(mBooleanIsPressed) {
mBooleanIsPressed = false;
handler.removeCallbacks(runnable);
}
}
return false;
}
});
Upvotes: 3