In an application I\'m working on, I have the requirement that a user must click & hold a component for a period time before a certain action occurs.
I\'m curren
I found this method. It is so simple.
private static final long HOLD_PRESS_TIME = 1200; //ms unit
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_control);
//Declare your button and add listener
ImageView iconImg = findViewById(R.id.more_icon);
iconImg.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()){
case MotionEvent.ACTION_DOWN:
Log.w("Button", "Button is pressed...");
//Start timer
timer.start();
break;
case MotionEvent.ACTION_UP:
Log.w("Button", "Button is released...");
//Clear timer
timer.cancel();
break;
}
return false;
}
});
}
private CountDownTimer timer = new CountDownTimer(HOLD_PRESS_TIME, 200) {
@Override
public void onTick(long l) {
Log.w("Button", "Count down..."+l); //It call onFinish() when l = 0
}
@Override
public void onFinish() {
//TODO your action here!
Log.w("Button", "Count finish...");
}
};