Dan
Dan

Reputation: 11

How do I write in android studio: If condition is true for at LEAST 5 seconds, then do something?

Below is my current code. However the problem is the image keeps flickering on and off.

    CountDownTimer mCountDownTimer  = new CountDownTimer(5000, 1000) {
            public void onTick(long millisUntilFinished){}
            public void onFinish() {
                image1.setVisibility(View.VISIBLE);
            }
        };

    if (dan > -1) {
        mCountDownTimer.start();
    } else {
        image1.setVisibility(View.GONE);
        mCountDownTimer.cancel();
    }

Upvotes: 1

Views: 50

Answers (1)

chand mohd
chand mohd

Reputation: 2550

var isFalseAfterDelay = false
 if (!isFalseAfterDelay) {
      isFalseAfterDelay = true//this will be true till 5 sec then it will false
         Handler().postDelayed( {
            isFalseAfterDelay = false
              },5000)// this will delay your operation for 5 sec
            }

Upvotes: 1

Related Questions