A free video tutorial from Atil Samancioglu
4.7 instructor rating • 20 courses • 211,719 students
Learn more from the full courseKotlin for Android O Development: From Beginner to Advanced
Learn coding and designing Android apps with Kotlin while building real app examples like Instagram and Foursquare clone
15:36:40 of on-demand video • Updated October 2019
- By the end of our course, you will be able to create professional applications and submit them to the play store from scratch!
- You will also have the skills of a professional developer who can earn thousands of dollars creating applications
English [Auto] In this lecture we're going to learn a new concept called runabouts. So it is very similar to countdown timers where you going to do the same operation within a time interval again. But instead of counting down we will count up this time I create a Krown I mean there are something to say. So let's name this project we're on the Bulls Jay or Annabelle's KOPPLIN And I will just choose the default set up empty activity and everything and wait for my project to be ready today. And we will use your animals again within games within projects of our we need to buy We need something to happen within a given in terrible repeatedly. K so this is again an easy concept but very helpful very essential in Koplin programming. So let me look. Delay out we can not see Hello world again. So I will go to build upgradeable and change this beat to to beat one. And as I said before countless times if you can see the halo world when you open the Android studio you don't have to do this. OK. So we now see the hello world. And we're ready to go. And let me make these hello or a little bit bigger so that we can see better. Let me click on this one and let me change the tax here that not only this and the tax will start with zero. So give me a bigger tax year maybe even bigger Turley's OK. Let me centrally airline this one. I think we're OK. Right. OK. So let me go back and change the ID of this. Don't forget to give IDs to your objects and let us bring in two Budenz one for starting one for stopping. And the first button. K. let me change the tax to start and the matter on click function will be smart as well. And this will be either stop or set and function again or a set. So I'm trying to build that quote meter where we start and stop the time. So let me start is in background so that our emulator will be ready when we try to them we are ready to test this project and the first thing they need to do is to create functions. So first one is strt function that is called by you called you and the other thing is grisettes. Here we go. Your function is reset here. So again we remove and called and look. Here we go. Now we are ready to implement around the bowls. So how do we do it. It is very very similar to countdown timer but we won't have any limitations like counting down our starting point or something when we hit start. It will start when we hit the reset reset it will stop. So we have to create some variables before we start. I will create a number. It's going to be an integer and will start with 0 and we will display this number within our text view and we will use this number under start sight. So I have created this under class here Kate and we will need a handler. This will be a handler and it will handle our operations regarding Annabelle's K and it will drive from handler class and the last one is actually runnable itself so it will be around the boat. As you can see when you say runnable It asks some coding section but for the time being we can just leave it like this because we will use and define discernible on the start and we will use the same runnable under reset by them for as well. OK so let me delete this number for right now and for a start. Let me say is 0. Each time we start the number will be reset so we will work with as handler and run the boat. How do we do it. We say runnable is now another thing. K we initiated runnable But now the ramble will be an object and it will inherit the Runnable class and be used around the BL members again. So it use some error to implement members and we have only one member here to implement. That is all right. Function run. So this will get called when a runnable starts k. They were rewrite your will happen. Them are runnable certs So what do I need. I need my text for you to be time plus my number so it will initially be time plus 0 time. Time is now 0 and then I have to increase the number why you want K and then what do I have to do. I have to actually refresh the text field because my number is now changed. Right. So I will say time is number again but now number is something else. So I will say händler dot. Post the light so it will ask me two things. First thing is to run the so I will do it for this time. This now this refers to the parent and the parent in this case is around the Belnap the main activity at the time. OK so we're in another block another completion now the closure now are this is runnable. So delay milliseconds is interval. And again for one second you say 1000 and you have to add this händler that pause. And what is to pause is the around the bill itself. OK. So this is how we start a runnable This is how we operate there and the runnable and under the code you write what is meant to. So here we go as you can see our Crolla me works. So it just turned it to count up but we can stop it. So in order to stop it we have to go to reset function. Right so that we can reset the thing. And I will start with. Time equals zero. You know you're to have a consistency and under-researched. What do we do. We do two things right. We have to first kill the timer. So I will say handler remove callbacks and the thing is to remove runnable itself and I will say number is zero. And again I will say text 3 that tax is now time is zero or time plus number because number is already set to zero. So don't forget to kill the Runnable. We do move called back because it will just reset to zero and then continues to start up. If you don't remove it. So you would go timer six seven eight nine everything. If I hit 0 0 1 2 and it starts from beginning as you can see it is very convenient. We don't have to use it with time. We can do what we want within a given period of time. This is 1000 milliseconds Cade's about we can change it with runnable so don't forget these are very powerful tools to implement in your own mobile applications.