The scenario is like if the user touches a view and holds the view for a specified seconds. something like a long focus listener but with a specified timer if the user takes the finger off before the timer then it won't invoke action. Is it possible? Please guide.
public class MainActivity extends Activity {
// This example shows an Activity, but you would use the same approach if
// you were subclassing a View.
//Declare timer
CountDownTimer cTimer = null;
@Override
public boolean onTouchEvent(MotionEvent event){
int action = MotionEventCompat.getActionMasked(event);
switch(action) {
case (MotionEvent.ACTION_DOWN) :
startTimer();
Log.d(DEBUG_TAG,"Action was DOWN");
return true;
case (MotionEvent.ACTION_UP) :
cancelTimer();
Log.d(DEBUG_TAG,"Action was UP");
return true;
default :
return super.onTouchEvent(event);
}
void startTimer() {
cTimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
//you can keep updating the ui here.
}
public void onFinish() {
//this is where you want to do something on the basis on long tap action.
}
};
cTimer.start();
}
void cancelTimer() {
if(cTimer!=null)
cTimer.cancel();
}
}
you can look ahead for MotionEvents at the docs.