erfan erfan - 19 days ago 5
Android Question

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.

Answer

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);
}