Android CountDownTimer with an Example

Android CountDownTimer is a timer which is used to decrease the time on the basis of our requirement.

It also provides the finish( ) method ,After completing the timer we can set any value or perform any task.

Some important method of CountDownTimer

onTick( long millisUntilFinished):In this method we pass the countdown milliseconds.

onFinish( ):After finishing the countdown ,this method is called.

start( ):It is used to start the count Down

cancel( ):It is used to cancel the countDown

Let’s start

Here we will see CountDownTime for 30 seconds.

Example:

0:30,0.29……..0:0

Code:

new CountDownTimer(3000,1000)

public void onTick(long millisUntilFinished)
{
textField.setText(“seconds remaining: ”,+ millisUntilFinished/1000);
}
public void onFinish( )
{
textField.setText(“Done”);
}
}.start( );

Output:

0:30,0.29……..0:0

Thanks and Regards

Placement preparation

Leave a Comment