Kotlin for Android Development : Develop an App with Kotlin | Sameer Niazi | Skillshare

Kotlin for Android Development : Develop an App with Kotlin

Sameer Niazi, Programmer

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
23 Lessons (1h 6m)
    • 1. Introduction

      1:09
    • 2. Installing the Kotlin Plugin

      3:26
    • 3. Variable Types

      1:51
    • 4. Find Views By Id

      0:38
    • 5. Kotlin Extensions

      1:45
    • 6. Strings

      1:21
    • 7. Open Weather Map API

      1:09
    • 8. Weather Class

      2:01
    • 9. Functions

      3:54
    • 10. Class Extending

      1:37
    • 11. Displaying Fake Temp Results

      3:54
    • 12. Improving String Formatting

      1:43
    • 13. Data Classes

      2:14
    • 14. Retrieving JSON From OpenWeatherMap

      8:28
    • 15. Presenting Data

      2:41
    • 16. With()

      0:53
    • 17. Null Safety

      3:21
    • 18. Enter Zip Code

      3:20
    • 19. Lists

      3:12
    • 20. Filtering Lists

      3:22
    • 21. Finding List Count and Average

      3:21
    • 22. No More Nulls

      3:51
    • 23. Kotlin Extension Functions

      6:27

About This Class

This online course will teach you how to use Kotlin for Android Development while creating a PRACTICAL REAL WORLD APPLICATION!

  • Do you want to learn Kotlin, the newest programming language for modern, multi-platform applications?
  • Have you spent time and energy trying to figure out the differences between using Kotlin versus regular Java?
  • Does your code need the structure and framework only Kotlin can give, but you don't know where to start?

If you answered yes to any of these questions, you're in the right place! 

This course was created to teach people just like you how to easily get started with Kotlin.

Kotlin is the best new programming language for Android development on the market right now, and we have developed a system to allow you to learn just what you need to know, only when you need it. 

Have fun while learning! Learn by actually doing!

After enrolling in this course, you'll be taken through a practical project that you'll use to create your own applications. Follow along and practice while learning!

In this course you will create a Weather Application that will pull real world data based on your zip code. While building out this simple but amazing application, you will learn the enhancements Kotlin provides while coding.

Whether you just want to enhance your programming skills or have a specific project you need done right this instant, this course is for you. We will guide you every step of the way and are here to make sure you succeed in your ventures. We are always available to answer any messages, discussions, questions, or feedback that you have.

ENROLL NOW

What you’ll learn

  • At the end of this course you will have a strong understanding on using Kotlin alongside Java.
  • You will create a real world, practical project along the way to make the educational process fun and interesting!
  • After completion, you should have a good decision on if Kotlin is right for you.

Are there any course requirements or prerequisites?

  • This course is a beginner's course to using Kotlin for Android Development, and isn't suited for anyone who already uses Kotlin.
  • Students should be able to navigate around Android Studio and know the basics of writing in Java.
  • The course presented is not intended to teach you to program; further education into general programming should be found outside of this course.

Who this course is for:

  • This course is for anyone who has never used Kotlin before and wants to see what it's capable of!
  • This course is not for those who are already familiar with Kotlin and feel comfortable using it in your workflow.
  • Students who want a beginner's glance at using Kotlin for Android Development should check out this course!

Transcripts

1. Introduction: I'm Ted. Whore is in. And welcome to the android Colin. Developer course. Colin work side by side with the Java. So that means you can have both Colin and Java within the same map. It reduces boilerplate code so you can do a lot more with very little road. It helps reduce no pointer exceptions with something called no safety. In this course, I'll show you the basis of Colin and how to create an android app. By the end of the scores, you should know how Colin works. How can reduce your bullet play code and how you to be more productive with android in this course will be learning how to create this weather. App will be fetching the data from open weather maps so it will be making requests to the Internet will be implementing a sing tasks so we don't block the main thread. We begin the weather with our zip code. If we put in a zip code, we should get the weather you low in the high 2. Installing the Kotlin Plugin: we'll get started by creating a new project. They'll name this weather up. It's gonna have a empty activity for right now. I'm going to create. It's tragic. Now we just need to install the column plug in. So just press the shift button twice, but in plug in begins, Scotland go to browse. Airport depositories. It should be Colin right there. I just installed that on your main activity. Just press shifts twice. You confer Colin version of a file to Colin File. Now, I want you to clean your projects and maybe read a little bit, but it's gonna pop up to show up. Look at this pop up called Connolly. Not not configured the configuring this now and choose your um you're Colin version press. OK, now, Colin should be inside your griddle. Now you see right here our compile Colin. And if you go to a project build, we'll see that are Colin versions 1.1 point avery and it's also inserted in here. It's going to sink that now. Okay, now we have Colin installs and writer to go. We have Colin in our main activity, and you can actually see whether a classes Colin or Java by this little K. So if we create a Jammeh file, I would just be a regular. See, Colin has always ended with Katie. It always has a K logo right there. 3. Variable Types: in Colin. We don't necessarily have to specify the type, like in Java or are variables. We instead have things like mutable and immutable variables. What I mean by that is, if you create a variable bow, it's something that's immutable. So you can't change the variable within that. If we put name equals Ted and we try and change that name, too Bob, for example, we'll get an error saying that Valley cannot be reassigned. This is what I mean by immutable ballot cannot change. Its entire objects were value, but Barr Cannavaro is immutable. So if we change vow to bar, we're able to now change the variable. We can also specify its type. So let's say you have a whole bunch of variables and you aren't sure what they are exactly . You could always change the type. You can always specify the type. So to do that, we just put a colon and then quit string, for example, or we can change this to a in nature. Do we get this little squiggly line? You can change the name to it, so specifying its type is optional 4. Find Views By Id: with calling. It's a lot easier to get your views from XML. You don't have to actually create casts with text view and but in to get your view. If callin all we'll need to do is do a fine view, but I d. But in your I d, such as current temp, but in your very evil you will then. But there's an easier way with Colin Extensions, which gets rid of this and you can just but in current temp, and I assure you that next video. 5. Kotlin Extensions: we can get rid of this fine view, but I d and just reference current temp Our i d with android extensions. So we have to do is go to our app, Riedel, we'll talk radio over the top and put in apply plug in Colin Android pensions. You sink. Now, This would give us the ability to just delete this and reference our i d from our layout here. You current temp and we're done. We don't do our fine view, but I d anymore. We can just do contempt that text equals are there for example And it will change the text with Colin. And there are no getters and centres, so we don't have to put set text. We just put text equals Hi there if you want The tax features do current temp dot text So he wants text. You do just that and he would have string now We just changed the text right there A lot easier with extensions 6. Strings: we can actually organize variable strings into another string a lot easier and a lot more readable. For example, we can create a greeting with these two bear evils. Hi, my name is and we can reference my name with a dollar sign and name. And I'm each old. But now that as a greeting. And now we have that in here. What happens if we need to get something lake? If we were to have a model and we need one of the parameters, we would just wrap it these and then just put it the parameter, for example. 7. Open Weather Map API: So now we need data for our weather app. The perfect AP I for that would be open weather map. There it is. Right here. It just, um sign in and go to your A P I keys. Get your e p I key. Go back to your A p I and we're gonna be going to the code What we're doing, we're changing samples to a p I changing our a p i e to our a p i key. And we're putting in units equals and cereal. Yeah, I have to. Every information we have banner max and her temperature. 8. Weather Class: need to organize their data now. So we need to create a class that could hold that we're gonna create a class called the Weather and where we only are interested in this main right here. Yes, I see. We want the 10th that men and intent Max, and that's it. You can create a weather class with a nested class. Inside of that, all the main and men. 10 Max. We just kind of created class here. Same as usual. Except we don't go to Java class. Do Colin file this class better weather now, with Colin, it doesn't by default give you the code to create the class. You have to create yourself so class, whether you don't put public class whether, um with Scotland, the regular classes. Same thing is final with, um, Java. Whenever you want a class that you can stand with, you just make it open. They're by default closed, so you can't. So other crisis can't extend whether we're just going to create a, uh, our main don't need the key. Worry with mean, um, with creating an object. We don't need the the new keyword I could empty. You don't have to put the type. I prefer playing the type only because it's ah well, easier to just skin. What exactly is a message below zero? 9. Functions: Now we're looking at functions with Scotland. We need to create a function with fun word. And we're just gonna create greeting very simple greeting parameters, and we're gonna put in our string. But we're gonna put in air text and make it a tape of string with this Colin, the text is type of string. If you wanted Mawr, you could always put just a calmer than other text drink. We only need one. Found your print line in the text. Test this out. Uh um Okay, this is so we now have our hello there. We could do so much for more than just that. Let's try and make a math problem. Let's try and have a someone and they come to you're just gonna do a, uh, some and we're just going to return someone less not do. Our return type isn't to the left of our function name. It's actually to the right. You're gonna do the same thing as when we're declaring a type. We're declaring a type for this function, creating the return type of int. And when you have a treaty, um um we're in credit answer of the some, but one plus five Iran, Do a line and, uh, answer and see showing right there were easier. Our answer is six nice and simple. Colin is also nice and simple. 10. Class Extending: We're gonna be diving deeper mawr into classes now. So we have an animal class and we have a walk so each animal can walk. It would be of a dog, and we can extend that. We're not gonna do it like driver. Where we do extends, we simply just do the return type. Um, the same how we would do it with functions. But this is with extending. There were extending animal. Now the problem is, Is that animal the class in? It's by default final. So we can't extend animal right now how we can change that as we could make animal open. And that makes it open to be extendable. So now we can extend animal, but we have to invoke its constructor first. Now, when we do dog, we can call walk and everything. And But what if it was in an interface? It must be different, right? Well, it is. Interfaces don't need to be open already opened by default and we can override walk 11. Displaying Fake Temp Results: we're gonna popularly, our weather class and we're going to present it on our android app with their current temperature are low in our high temperature. First thing we need to create the weather class. Now we need the main class. I need to populate it with the temp, the midden and max. So with Colin, we don't need to to create centers and getters. What happens with Colin is we have a bar of temp and that gives us the ability to set the the very bowling. Also get the variable. It automatically creates the gators and centers. That's why you can do a temp equals 100. Or you can dio um, you can still get it this way as 90 Max temp of 100. That meant MP fools. And 20 that doesn't look realistic, but And now we're going to but the main inside the weather that me There we go. We're now going to just present our data present data creator function And what, um, as we need every current tempt, low temp and height, him temp, not text. I mean, I remember again, we don't need to set texts because texts you look a text from the text view. We're actually getting the text or we're setting the text already. It's working just two equals. I mean, uh, um, temp bring for that. No, lo temp. That text was, uh oh. Temp. I mean, no high temp. Oh, Max. Okay, string here. Now, we should be good to be 90 acts of 100. And there we are. We have the 90 the 20 now, the 100. 12. Improving String Formatting: we can populate these text use a little nicer with strings, for example, weaken, Do a text bulls. You can actually put the variable inside of our string, and it will automatically change it to a string. And that way we can do this. Plus, I have a degree sign re symbol. Just do that. Do the this for all of them. - Now you ever strings. And now we're going to see if it looks right and it works. And now actually looks a lot nicer now, because now we don't have this to string. And now we want to do plus degree. We just put the, um, the dollar sign, and it just looks a lot nicer this way. 13. Data Classes: we're going to creating something called a data class and pretty much a day or class is something that only has a constructor and automatically makes the Gators and centers for you. You don't need to create any variables or anything. It creates it for you within the constructor. I'll show you what I mean. First, we need to put data behind class and having person. They are constructor through the class. We can have our constructor within any class near the you were class instead of putting no , um um, instead of having person and then putting our variables in there, we're creating it over here. Let's say we had we want getter and setter. Throughput bar would put, um, aim string. But not only that, we can create a default value, so we want a default value of ah, unknown, for example. Then we can have a age, didn't and we could have a default value of zero, and we Yeah, we can have this will automatically create the gators and centers, and I'll show you right now be put in, um, Ted and 20 we can do things like get the the age in the, um name automatically. Now, you know, if access to the age and the name and change them everybody want now And that state of classes 14. Retrieving JSON From OpenWeatherMap: We're now going to get RG son from the open weather map. So I inserted retrofit in Ji sahn for this project and I created a American client which pretty much grabs my i p a my a p I key. And since a, um, request to gets the whether from my sub code and the zip code is I'm a customizable. I did create a interface that is a get request and has query of units. Ah, that Then you're a P I key. I also made sure to put in the permissions to get to use the Internet from a P I key in the strings. Um, that's that's it. And now we'll just call and get our ah, Jason first thing, we're going to get our we're going to do function, maybe. Ah, color never clients. They're gonna call the the function that will get our Jason earn a zip code. Ah, that's a good one. Surrounded one. Uh, Now we'll just, um, that's our call. So we'll do a ah call in Q. I don't think I assure you this before, but whenever you need to implement something likey likey on click listener inside of a um, a function like this. You can't just do new in the new callback, for example, or a new on click listener got to do is just do objects. Then do call back whether a anonymous inter class that that's what's called second there. Well, let's do it getting so I was talking and okay, first we do objects, then we put in our what we need to implement. Um and we just parentheses and we quick on her object to weaken implements Are things right in there now these to do's with Colin with java A to do, um, we'll let you compile him and run the app, But in Colin, we have a to do. It won't compile. So just make sure you u, um doing of those and put whatever you need in there. I like the idea of having a to do in there. I'm forcing you do that to Dio so you don't forget to put in. You know, we don't forget Teoh Prince, the factories forget your throw balls And don't forget to actually handle the data. So we're just gonna print ah, when they were going to get a our weather first again I'm going to really quickly. This this isn't really editorial about retrofits is this is a tutorial boat Scotland going pretty fast with this. Now, you see here we since weather since my response can be No, we're actually putting a question mark here saying response brushed remark, saying, if response is no, just make this no, and just skip it. If it is, continue to body and return the body. Now, whenever we use weather, we're going have to know checked as well. And we do X temp. Yeah, What's to temp? Now we have to do the question marks in here. And these are individual nolde checks. So doing whether the main question mark there is the same thing as doing If whether that main does not equal no Then do whether that main attempt is there a pretty much just checking no checking here. Finger it here. And the best thing with this is that we can do something like begging for no here. Well, now we don't need that question mark anymore. If we do something like that, isn't? That isn't know either. And we don't need thes these question marks. You can also say that That's not know as well. We don't need that. So the double exclamation points means, um if it is no is gonna throw a no pointer exception. You shouldn't be putting these in here, But I do have a Doesn't equal no. Here. We're just going to from this run, I will describe the this no safety thing in another video. Okay, but problem here. Jesus. Firstly, floats also put at serialize name here. That districts means that it's, um, matching the 10 max with the Jason that again. Now we have a temperature, and that's it. 15. Presenting Data: we're not gonna change the text within our app to the current temperature. We're going to get our It will lay out text views or current Templo temping High temp. Uh, we're gonna get our temp, our specialist, to this but the main and been lutin and all that. Oh, temp. And we're gonna put these round parentheses in quotes, not parentheses. Uh, we're gonna put in that. I agree. And this, well, current imp. Now, let's see three. It be different. There we go. We have heard regular temp, we ever low and we're high, which but in I lo and we're finished. 16. With(): it's now we have our data on the screen, but we can do a little bit more to clean it up. The first thing we're gonna be using the prison to data function instead of by hand here. His attorney there. There's one thing called with what with does is if we put made in here. Now what happens is we don't have to put main dot temp main that men in maine dot max it automatically does. It gives you main and then just have to do the, um, create. Um, I used to function. So now we don't have to do pain that tempt me and the men it made that Max. 17. Null Safety: We're gonna look at no safety now. So with it no safety. It automatically creates a no check for you. For example, here if response that body equals no. Because of the the double exclamation points. If that is no, it's gonna act exactly like Java. Just gonna throw a no point exception and crash. You don't want that. So we want to use double exclamation points as little as possible. What? We can use this. That is question Mark, which what question Mark does ironically creates a no check for that. So it takes response and says is body no. And if body is no, just make weather. No. Now we have this thing for variables. If they are no. So we put a question mark the side of it, indicating that weather might be no, his body, I'd be no. And now, whenever we re use, whether we have to put a question mark whenever we want to call a function within it, because the weather might be no mean might be Noah's. Well, if Maine is no, this would turn no. And then with main, we Since this might be no, you can't call function with a big. No, we have to handle that. Begin Alz right. Click on all to enter on it and we can either stranded with no check. And we can do that's ah as three lines. It doesn't look very nice. It's in the way of everything. We also have something. A nominal check. So that means I don't care if it's No, Just do this. If we do present data it's gonna give us, you know, knows across the entire app of the texts the Max temp and the mint imp. We don't want nose in there. We'd rather have just past data and nose. We're not gonna do that. Well, we could do is let what let does I'll show you. We take mean and we see, if mean is no, exactly exactly that. Let me, uh, go back to and then so that would take. You mean and we're seeing if Maine is No. So if Maine is not know, run this. But if it is no, skip it. So we will only do this if Maine is not know that is no checking, no safety 18. Enter Zip Code: we're now getting the sick code former edit text and calling this function, getting our weather first thing, we need the co text and we're adding, just like in Java at text change listener. But this time we're just going to do the object again and text lecture or, uh, moment, all of these we're going to after text. We're going to We're screening this too Kotex. Uh, just at it. It it next bulls that and will do you if text, uh, length equals five, which is the size of a pallets of code, and then we're going to call get weather from zip code. We're just gonna put in our text over renamed that to code. I would, but this isn't int as well. Now we get to wait and see if it works. No, and something happened. Now you have to get rid of these to do's every time I'm not going to use them, so I'm just gonna get rid of them. But it is nice to know that you do have questions here that you haven't used and actually events you from even running the app. So you actually have, um you know that you didn't do a method right now if you put in a Rams code done and it automatically did it for us. 19. Lists: We're going to be running our code with unit tests from now on to make this a little quicker. But we're broke. I would be looking at list of immutable list of to Correa lists. So I'm going to test Java and then your package name and then discreet a random Ah, last I need to my example and Tester. We're going to use this one class for everything for a few a few videos. So to create a regular list, we can do it. Sure hand with list of. We can create a list of um, hence, for example, and with lists of It's actually just a list and it's a generic types. We have int along any object we want. Downsides of a list are they're only readable. We can't change any of the information inside the list. So if we get list of Index zero, a sneak was one. We can't do that. It won't let us. There is no set method. All of the variables and indexes on here, all bowel, they're not virus. So we can't change anything in here. We can jittery over them. We can get in a for each, then print thes out we can Ah, and those We'll have one through four. We also has something like a beautiful list, which mutable means we can edit it. So we have read and write access. We can take the first index and we can plus equals 20. And the 1st 1 will be 21 as we just changed it right here. We can also add big number. And we can also remove where we remove different ways so we can remove find elements we can remove three, and this will not remove the index three but the index with three in it. If you want one too, deletes and index. Then we just to remove at and I will remove the index of that value. Now Index one is gone. 21 not there anymore, and that's mutable lists and lists. 20. Filtering Lists: How do we filter a list in Katelyn? Now, I'm gonna show you how to filter list within Java first and then show you Colin. So in Java we would re another list the mutable list of it, but int we would generate over our immutable list. And then, you know, if and put in our filter item is greater than two, for example. And then, Peter, you're the mutable filter list. And then add our item. That's how we do it in Java first. A lot of cool right here. We have, you know, five lines of code around Catlin we are doing. We're taking our immutable, and we're just putting filter on it. What this is doing is this is doing exactly over here is this is Tiki each item, and then we're going to see if item equals to two. I'm a greater than two. I mean, and this is going to give us our both period beatable. Give us the same exact results. Um, and only just one line of code. We're gonna verify that it actually has those, uh, those results that are greater than two. We're going to do a four loop on it to see what's inside of the list that we just filtered . - Now we can see we have our list with 34 and five, which is greater and to and we can also do things like equals equals two. We can change that as well. So in this sense we would Onley get to. 21. Finding List Count and Average: we're now looking at counts and also average so with count, we can actually see how many of the items are within a certain filter. So if he filter into greater than two, we'll see that we have three now show you the every person of it first and then the Colin version of that. So first, we would create a count of items. Amiable list of that, uh, and we re take your immutable and then for each doing a for loop over our mutable lists. And then if I am is greater than two. I should only the kind of items we just need count zero industry accounts plus bless. Every time there is something that scared into, you're going to increase our counts. And we had to meet the mutable so we can change that. Let's do with the Colin way the easier way. I'm just gonna do mutable darts, count. We're gonna put in our filter here just like filter. When do item better than two and get accounts? Uh, Bredar, you We can print that, uh, counts credit to and really over, and that's a lot easier than the just regular during the four loop and you're in that the beautiful that count is a lot easier than doing the four loop. Bring your own variable of counts in adding, so that we can just do it this way we can. Or three. Let's have another one called average the This only works on, you know, in the and double wasn't float lists that this won't work for no objects. We can do average on that. And we will get the average of all these numbers and we have our average. And to do the same thing, we would have to create another four loop and then another variable and then pretty much calculate the average that way. Um, I doing that, you know, fibers excited. The code. This is one line of code. 22. No More Nulls: we said we haven't known this list. How do we get rid of that? No, Avery. Easy way where there's a function called Phil tonight. No, that will give it of these knows for you. But I'm gonna show you exactly what it does in Java Before we do the Palin version en Java . How this would be done. It would it would do a for each on it would have an item like Is that like this? But this is a easier he's away with for each. Then take, we then create another, um, list list without knows, equals and beautiful list of to say and T list. That's an ant lift. Now, if item doesn't equal no, we then at that's the list list. Without knows, he's gonna add to that our item. That's exactly what the hell tonight. No does. It's four loops over the terror list says if this item is a no, put this into the another list, and then that's pretty much it. Now go and do a filter National and Colin Grant filter knots not know, and that's it. We can see that it is pretty much the same thing as filter except is doing a filter is returning another list. So we have to do is create no list called list without nos, we're going to do another asserts asserted that certain equals that we're gonna do a mutable you to a list of with 123 and six and one with 123 You know the list without nos. I'm gonna see if that works. Now, when do when doing this? You could also do a regular filter, such as doing just a regular filter here. Um, and the item does not equal No. And that would essentially be the same exact thing. It will be more light, more co two rights. Um, this does that for you there. We could see that. 23. Kotlin Extension Functions: We're looking at extension functions now, so accession function are something that we can create. This is creating our own function for a class. For example, we can have, um, people So our immutable list and add a average function exactly like this where we can have something like a having a string and then creating another function like, uh ah. Like a fancy split that we have the lamb dad like this We can do that. We can create Lamb does with that. We're gonna do that really quick rate. Now the first thing we need to create the function. Second, we need to create the generic type visionary type I like to put t. And now, since people list is a list, if you go to that and we go todo lists election, we could see that it derives from literal. We have to put terrible as the last name. Now we're going to create a function from that class. Terrible. So it will. It will work as extends from interval with people, which is list. I'm going to put average and we're pretty much from to make exactly what is this gonna get ? The average of the age. We're going to teach eight average of all of them and calling that we don't have that. We only have averages with the It's a list of numbers, not with object. Now we have our predicated. Now we have We have a parentheses and what this is is, um we put t there. What? That means we're giving predicated a type of tea inside. Apprentices. This is saying that b wants t so person inside of this this land of code, we want this type person, and he wants the this bean ent. Okay, t the parentheses is, um what? We want this return. And this is just what we want to in a simple A set. That's what that is. Now we're going to get our the number of the size of our list first, some of items. Well, no, we can't do this start size because it's now list. It's a miserable Richard. We're just gonna do a stock count. Easy. Um, discount. We have accounts of every item in. We're gonna get the sum of all of the items in there. Rates over them, uh, items. Now we're going to take predicated. So what we're doing is we're going to take. Okay. What critic it is is this. So with each item in here, we are going to say we want to call this function. But we're saying instead of predicate we're doing under the hood, we're doing elements that eight or we have to give that element to it. So we're gonna pass in element. A ticket is pretty much just a pointer. Do this. And we're just morning in the data to our elements, or this is just a person my age, and Colin is telling us we haven't easier way to do this. We're going to listen. You have a son by. Yeah, and there's, um some of that. They were what we did before in return, move items divided by a number of items and that should our average and type line of ever bring. Now we have our average we can. And just as well we can add another person. Uh, and, uh, agent. It was down to 30. And we know what extension function