0

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();
        }
    });
Ahmet B.
  • 1,290
  • 10
  • 20
  • 1
    This may be helpful https://stackoverflow.com/questions/57212171/how-to-handle-double-click-on-event-onclicklistener-in-android-studio/57212192#57212192 – Shalu T D Jun 14 '20 at 16:48

3 Answers3

2

I recommend you start a counter that gets reset every 60 seconds.

Try something like this:

  private int counter=0;
  private Long timeSinceLastClick = 0;
  private boolean checkTime(){
     if(counter == 0){
        timeSinceLastClicked = System.currentTimeMillis;
     } // if our counter is zero, we start a timer. 
     counter++;
     if(counter < 2){ // if our counter is less than two , then we return true.
       return true;
     }else{ // Otherwise we need to check if 60 seconds passed. 
             long currentTime = System.currentTimeMillis();
             long timeDifference = (currentTime-timeSinceLastClick)/1000;
             if(timeDifference > 60){ // been more than 60 seconds.
               counter =0;
               timeSinceLastClicked = System.currentTimeMillis;
               counter++;
               return true; 
             }else{            
                return false;
             }

     }



  }
0

You can use Thread to do this.

int counter=0; //globally declared

In OnClick Method

if(counter==0)
{
 Thread th=new Thread(new callthread());
       th.start();
}

counter++;

if(counter<=2)
{
//TODO
}
else
{
Toast.makeText(context, "You can use this property only two times in a minute", Toast.LENGTH_SHORT).show();
        }

callthread method

public class callthread implements Runnable{
    @Override
    public void run() {
        try {
            Thread.sleep(Interval time in millisec);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        counter=0;
    }
}
Sahil Goyal
  • 415
  • 3
  • 13
0

Just keep a single counter variable. Then use this code, when the counter's value turns to 2

new Handler().postDelayed(new Runnable() 
{
    public void run() 
    {
        img_number_search.setEnabled(false);
    }
}, 60000    //Specific time in milliseconds

);

This code disables the button for 1 min.

Soumik
  • 55
  • 1
  • 8