Welcome back and this video, we are going to add the timer functionality, which allows us to display
a timer in here, which will count down from 10 to zero.
So by the end of this video, we should get to this point where you can see it's counting down and the
number is displayed and at the same time here, our progress bar is depleting.
All right, let's get started.
And in order to get to that point, we need to implement the functionality in our exercise activity.
So in here, we, first of all, need variables, one of them to being a countdown timer.
So we're going to create a new private bar.
And I'm going to call this one wrests timer.
And it should be of type countdown timer, because I wanted the time to go down and to Countdown and
therefore we create a countdown timer.
I'm going to create a nullable here because I don't want to set it directly here and I still want to
be able to use it within the whole activity.
So the other one is going to be a simple integer, which I'm going to call rest progress, and I'm going
to set that to zero.
So the idea behind this progress is that it will just count up.
So instead of counting down as a countdown timer, it will count up from zero to 10, for example,
and that will help us to display the correct value.
All right, next up, we need to set up a functionality for our progress bar, so I'm going to create
a new function called Said Reste.
Progress bar, so our progress bar, if you remember, was this thing here, which is the progress bar,
which we call the progress bar.
All right, so this is going to be our own progress bar and activity exercise.
And now we can go ahead and set that functionality up.
And what we need to do here is we need to, first of all, set the progress of our progress bar.
So Progress Bar has a property called progress and we can set it to our rest progress.
So what is the rest progress?
Well, at the beginning it's at zero, so.
It starts at zero, and that's what it should be said here.
But of course, later on, we will set our progress somewhere else.
That's why we created this as being a field or a global variable here.
So now we can go ahead and set our rest timer.
All right.
Because we prepared the rest timer and now if we want to.
Go ahead and use it.
What we need to do is we need to set it as a countdown timer and how we do that is by using Object Kolon,
then countdown timer and then in brackets.
We can now create this object, so we need to have the Mila's in future and the countdown interval.
So medicin future, I'm just going to set that to ten thousand, which is ten thousand ten seconds or
ten thousand milliseconds, and then the countdown interval should be one thousand.
So we want to have one tick at a time.
So every single second one tick should happen.
And in order to now use this functionality of the countdown timer, we need to override Ontake functionality
so you can see there are two functionalities that we can override.
One is the on tick and the other one is on finish.
On finish is what happens or what should be executed once the timer is over and Ontake will be executed
every single tick.
So here, as we define that the whole time or should go for 10 seconds, that countdown interval is
one second will.
It will be that we will have ten ticks.
So every second there will be one tick until this time is over.
So now it has not implemented well, is it to do now?
We need to implement it.
And what I want to do in here, every single tick, I want to set the rest progress.
And I want to increase it by one at the same time, I want to set the progress, bar the progress to
ten minus the rest progress.
All right, so this means that the timer will start at 10 and if our race progresses one, then the
timer or the progress will be at nine, then then the next second it will be at eight, then at seven
and so forth.
So that's why I use this ten minutes progress.
Of course, this is all hard coded here.
We could go ahead and create variables for this so we could go ahead and create a new variable here
which takes care of the rest time, more duration or something like that.
You can do that.
But I'm just going to keep it simple because otherwise the the whole application will be huge in terms
of me talking about stuff.
I want to get forward and to really show you how to get things done.
And you already know how the best practices are, at least to a certain degree.
And you can see that I'm telling you every now and then how it should be done and what we are doing
in order to be quicker, so to say.
All right, so then the text that should be displayed as a TV timer, what is the TV timer?
Well, you can press control and click on it.
You will see that this this TextView in our activity exercise, which just currently set at 10, so
currently just displays the text ten.
But now in the future, I would like this to display something else.
So TV time or text should be ten minus.
Rest progress, but here am using brackets because I need to make it the string, because 10 minus something
is going to be a number and text expects a string.
So you can see we need to set the value as being a string.
Now, of course, we can also implement the unfinished functionality.
So what shall happened there?
Well, I'm just going to keep it simple.
I'm going to copy and paste my code in here because this is nothing new.
So tost make text.
The context is this add exercise activity.
The text should be here now.
We will start the exercise and those lengths should be short and we show it.
OK, so this is what should happen when the timer is over, when the ten seconds have passed.
All right.
Next hour will create another function.
Which will set our rest view or set up the rest of you.
So a private fund set up rest a few.
And the idea behind this is that it should call this said progress bar or set up progress bar function,
but it should do some other stuff as well.
For example, if we set the screen or if we get to this exercise activity we want to do is we want to
make sure that our risk timer is going to be canceled.
So if there was a timer running already, for whatever reason, want to make sure that this timer is
not running anymore.
So what we do is we say if the timer is not now, please go ahead and set the rest timer.
To be canceled, so just cancel the rest timer, so reset it, so to speak, and set the rest progress
to zero, so send it back to a default value.
And next, I want to call the address progress bar.
So this function that we created here, so this set rest progress bar, which had to tick and to finish.
All right, and then what we need to do is we need to call set up rest view at one point because that's
what this whole idea is.
So let's copy this and call it in our uncreate, because for now, I want this to be called directly
when our activity started.
And that's happening when the uncreate method is triggered by the system.
So it's just going to set up the rest of you and started directly.
And then finally, there is something that we shouldn't forget to implement, and that is the on destroy
method, so on destroy.
There is an override.
You can see there is a method called undestroyed, which is generally available for all activities.
And what I want to do here is I want to make sure that my timer.
Is reset.
So rest Taimur.
If it's not now, then please go ahead and set the rest timer to be canceled or cancel the rest timer
and the same goes for our progress, even though that should not really be an issue because our progress
we could see here when we get this activity, it's that set to zero.
So whenever we get to the activity, it should be set to zero.
But if you want to make sure that it really is, then you could use this in on the straight as well.
Otherwise you could, of course, also just say rest time or to cancel.
So why do we check if the rest time is not?
Well, because we are using the double exclamation mark and making this nullable or unwrapping this
nullable to make sure that we can call cancel.
Otherwise we would run into errors here and our program would crash.
Now, there is one thing that I have forgotten, and that is to start our countdown timer.
So this is the point where our countdown timer has its finishing Curdy bracket.
So what I will do here is I will start the timer here.
And if we didn't run the application and test this, we can go to start and we can see get ready for
and our timer goes down so you can see here it goes down.
This is the progress bar, the number.
This is our well, the number that we get from here.
So this is progress or better said, ten months rest progress.
But overall, as you can see, we have a basic timer here, which perfectly does the trick.
So finally, we have something happening in Europe, and you could see Ontake was called all the time
and then what also was called this unfinished.
So let's see it again.
Once we run this, we will see a little note here at the bottom.
So little post saying here we will start the exercise.
And we are.
So this is unfinished called once the timer is done.
All right, so I'd say see you in the next video.