Erfan
Erfan

Reputation: 3343

how can define timer count in java android studio

I want to define a time counter from 0 to 30 seconds in the onClick listener of a Button.
When the Button is clicked, the time counter starts and it has an if condition to check, for example, if timecounter=10. Then my ImageView becomes visible

This is my trial:

base.setVisibility(View.INVISIBLE);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {


        new CountDownTimer(30000, 1000) {

            public void onTick(long millisUntilFinished) {

                if(millisUntilFinished==5000)
                {
                    base.setVisibility(View.VISIBLE);
                }
            }
            public void onFinish() {
            }
        }.start();


    }
}); 

I want that when I click the Button, after 5 seconds my image view becomes visible.
If anyone can help, please do.

Upvotes: 0

Views: 143

Answers (2)

Swr7der
Swr7der

Reputation: 859

I think you should use 25000 instead 5000 because millisUntilFinished shows the milli-seconds remaining.

Solution:

Try this code:

if(millisUntilFinished >= 25000 && millisUntilFinished < 26000)
{
      base.setVisibility(View.VISIBLE);
}

OR

if(millisUntilFinished/1000 == 25)
{
      base.setVisibility(View.VISIBLE);
}

Upvotes: 1

Dileep Patel
Dileep Patel

Reputation: 1988

try to this code hope this can help..

new CountDownTimer(30000, 1000) {

         public void onTick(long millisUntilFinished) {
            if (String.valueOf(millisUntilFinished / 1000).equalsIgnoreCase("10")) {
                Toast.makeText(getApplicationContext(), "10 second remaining.!", Toast.LENGTH_SHORT).show();
                base.setVisibility(View.VISIBLE);
            }
        }

        public void onFinish() {

        }
    }.start();

Upvotes: 1

Related Questions