Search code examples
javaandroidimagebutton

How do you Add a delay between user press on imageButton?


I would like to make it so that the user has to wait a short time before allowed to press the imageButton, I don't know the best approach for this.

private class HandleClick implements View.OnClickListener {

    public void onClick(View arg0) {

        if(arg0.getId()==R.id.imageButton){
            ((TextView) findViewById(R.id.textView2)).setText("Pressed: " + ++howManyClicks1);
            /* Uri uri = Uri.parse("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=1");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);*/
            simpleWebView.loadUrl("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=1");
            //startActivity(intent);

        }
        else if (arg0.getId()==R.id.imageButton1){
            ((TextView) findViewById(R.id.textView3)).setText("Pressed: " + ++howManyClicks2);
            /*Uri uri = Uri.parse("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=2");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            //startActivity(intent);*/
            simpleWebView.loadUrl("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=2");
        }
        else if (arg0.getId()==R.id.imageButton2){
            ((TextView) findViewById(R.id.textView5)).setText("Pressed: " + ++howManyClicks3);
            /*Uri uri = Uri.parse("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=3");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            //startActivity(intent);*/
            simpleWebView.loadUrl("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=3");

        }
    }
}

I don't want the user to be able to spam click the imageButtons. Maybe the best approach is to add a delay or disable the button for a short time after a it has been pressed?


Solution

  • Try countdown timer inside your click listener

    new CountDownTimer(5000, 1000) {
    
       public void onTick(long millisUntilFinished) {
          //count down 5,4,3,2,1.
       }
    
       public void onFinish() {
          //Write you logic here
       }
    
    }.start();