Search code examples
androidbuttondelaycountdown

Button only clickable after X seconds


I am trying to create a button which is only clickable after for example 3 seconds. It should display a countdown, when 0 is reached, one can click the button and an action gets performed. For example when the Activity gets called, the button displays a "3" first and counts down to "0", only then one can click on the button.

Any ideas how to achieve this?


Solution

  • you should use CountDownTimer for that:

    timer = new CountDownTimer( 3000, 1000 ) {
      @Override public void onTick( long millisUntilFinished ) {
        button.setText( "count down " + millisUntilFinished );
      }
      @Override public void onFinish() { 
        button.setOnClickListener( onClickListener );
      }
    };
    

    somewhere down below:

    timer.start()