A button should be clicked only two times in a minute. If the user is clicked for the third time for example in 50. second , app will warn user via toast message. Moreover for the 4. and 2. clicks there should be also 60 seconds. I tried to write a method for this ,
int counter = 0;
long counterOne = 0;
long counterTwo = 0;
long counterThree = 0;
private boolean checkTime() {
counter++;
if (counter == 1) {
counterOne = System.currentTimeMillis();
}
if (counter == 2) {
counterTwo = System.currentTimeMillis();
}
if (counter == 3) {
counterThree = System.currentTimeMillis();
}
if (counterThree != 0) {
if (counterThree < (counterOne + (60 * 1000))) {
counter--;
return false;
}
}
if (counter == 1 || counter == 2 || counterThree > (counterOne + (60 * 1000))) {
if (counter == 3) {
counter = 1;
counterOne = counterThree;
}
return true;
}
return false;
}
and I want to use it as;
img_number_search.setOnClickListener(view -> {
if (checkTime()) {
// TODO
}
else {
Toast.makeText(context, "You can use this property only two times in a minute", Toast.LENGTH_SHORT).show();
}
});