1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Introduction to Android Studio, Kotlin, and Basic App Layouts

Kotlin Intro

Start course
Overview
Difficulty
Beginner
Duration
3h 39m
Students
1
Description

This course begins by downloading Android studio - make sure you watch the appropriate video depending on whether you're on Mac or Windows. Then, we'll take a tour off the Android Studio interface and see how apps are put together. You'll learn about text views, buttons, and images to build a user interface for our app, and we'll also write some code to make our apps interactive.

Then we'll move onto a practical project in which we make a temperature converter app, which converts temperatures from Fahrenheit to Celsius. You'll be able to follow, building the app, and then running it on your system.

Intended Audience

This course is intended for beginners to Android app development or anyone who wants to master coding in Kotlin.

Prerequisites

Since this is a beginner level course, there are no requirements, but any previous experience with coding would be beneficial.

Transcript

Now, before we move forward and start working with Text fields, let's take a mini break from Android and look at the Kotlin language. You've already seen a decent amount and we need to dive a little deeper before we move forward. And we'll do this from time to time to learn more about the language and try things out. It will also hopefully clarify some of the things that are happening within our app. So I'm going to do this using app Kotlin Playground provided by kotlinlang.org. So, to do that, pull up a browser Window and go to play.kotlinlang.org hit 'Enter' and this will pull up a Playground where you can test Kotlin code out and then run it to get output,

I'm going to agree to this. And we've seen a few things in Kotlin so far. Among them were strings functions which I kept calling a method types and we saw a view as a type as well, and also back to the Android Studio, a type of a main function here, which was onCreate and this was the main entry point for our app. Basically when the app would start, this is the function that would execute. Now when working with Kotlin or Java and a few other languages, you'll be working with an entry point to those respective programs. In many cases it will be called main. So, if I look at the Playground, you see this fun main. This is the main entry point for this particular program. For a regular Kotlin program, you can think of this main as equivalent to the onCreate that you saw in the Android project. So, let's start at the top here. First you see the / and * and then a * and a / and within it, you see some information here. 

So, this /* opening and the */ closing, this means multi-line comments. You can basically paste in comments for your code and whatever you enter here will be ignored by the program. You can also write one line comment using //. So I'm going to say "// This is a comment" and the program is going to ignore this when I run it. Then you have this main function as you have seen earlier already. It starts with the keyword fun for function and then the name of the function which is main followed by open and closed parentheses and then open curly brace and close curly brace and the function code goes in between. So whatever code you want to write for your function, you'd want to write it within this open and close curly brace. 

So, the code here that's written for us already is print line and then within parentheses within double quotes "Hello World". So, as we know from a bit earlier, since this is the main function and the entry point for the program when we run this program, it will start executing whatever it finds in here. So, if I click on a "Run" right here, you see an output terminal or console pops up and prints "Hello World" to the screen and that is what the print line function does. It prints whatever you give it to the console, and here it was given "Hello World". So, what is a string then? I've been saying strings quite a bit. A string is a basic data type in programming languages that's used to represent textual data and it's usually a collection of characters. 

You see a collection of a lot of characters here. And interestingly character is also a data type but it's usually just one character. But we're going to stick to strings for now. In Kotlin, you surround strings within double quotes to indicate that whatever you have inside here is a string. And I can write another string here or change this. For example, I can make this "Hello Mashrur" and if I run this, it will print this out to the screen. So I'll run there we go. Now it says Hello Mashrur. Now , instead of directly entering in a string in the print line function, we can reference the string by using what's called a variable. Think of a variable as an address to a location in memory of your computer. 

So, I can declare a variable, I can declare it by saying var or val and I will explain the differences very soon. For now let's just stick to val, So, val and then I have to give it the name of the variable. I'll call it greeting equals and then I'll set it to a string. I'll set this to this string right here. I'll copy this and put it within the double quotes here. Then instead of writing the string over here, what I can do is get rid of these double quotes and simply reference the variable greeting, which is the name I gave to my variable. Now if I run this, check it out, still works. Hello Mashrur. So, variables are a very important thing to keep in mind and we're going to be using them a lot, but the idea again is that think of them as an address in memory. 

So, there's an address in memory, I'm calling that address or location greeting and in that location this string is saved. So when I reference that address, this string is what pops up. So, I can access this string. And this could be a number as well, let's say 10. And then I can do the same thing here, run, and you see 10 shows up. And this is a different data type, this is an integer. And we're going to talk more about other data types, but for now we're going to stick to strings. But you see greeting should be something generic, not specific to the name Mashrur. So, one good thing about strings is you can connect strings, one after the other. It's called string concatenation. And what you do is you simply add one string after another with the plus sign. 

So, here instead of whatever I had before, I'll put within double quotes, "Hello, how are you doing today?" And then I'll declare another variable. I'll say val myName, and this type of naming convention is called camel case. Again, we're going to come back to this. Basically the second word, so my and then name. Second word is capitalized so N is capitalized, the first one is not. And again, we're going to come back to this later. So, val myName, I'm going to say "Mashrur". And then within print line I can say greeting and then plus myName, so the greeting bit will print this. "Hello how are you doing today" And then myName is going to print "Mashrur". So, let's see what happens if I run this, run. Hello how are you doing today, Mashrur. 

There's a space missing, so we can add a space there. So, I can either add a space here. So, it'll be a space within this string, or I can put an empty space here. I'll put double quotes, and then an empty space within the double quotes and then I'll have to add this back, so I'll put another plus. So, now it's going to say greeting, so "Hello how are you doing today" plus the space. So, we'll add a space after it and then plus myName, which is "Mashrur". So, let's run it, there you go. Hello how are you doing today Mashrur. Now, notice something that happened. I just named these variables but I didn't specify what type of variable they were. I said there were strings, but how does Kotlin know that? Well, Kotlin does what's called type inference. Where it in first what type you want the variable to be. So it inferred automatically in this case that I want these two to be of type string based on the double quotes that I have surrounding them. 

And you can specify the type by introducing a colon after the name of the variable and then the type you wanted to be. In some cases you might want to do that, but in general, Kotlin will make inferences on your behalf. So, if you wanted to enforce this as a string, you could say a collon after greeting, and string and then the rest remains the same. And if I run this, it'll still work. Notice also that when we were referencing the variable names, greeting and myName within print line here, we didn't include double-quotes for them. For example, I didn't do this, I didn't say, "greeting" but when we entered a string directly, which we did before or in this case the empty white space, we did include double-quotes. And that's because variable names are references to the memory location where the string resides, and if we put quotes around it like this, then this becomes a string itself. 

So, this is going to say, 'greeting, space, Mashrur' so if I run this you see it says greeting and then Mashrur. So, this instead of being a variable has become a string. So, I hope you enjoyed this brief intro to a simple Kotlin program with a main function, some comments, variables, strings, and type inference. And we're going to end our introductory look into the language for now and return back at a later time. And for now we'll switch back to Android Studio in the next video to work with Text fields, see you there.

 

About the Author

Mashrur is a full-time programming instructor specializing in programming fundamentals, web application development, machine learning and cyber security. He has been a technology professional for over a decade and has degrees in Computer Science and Economics. His niche is building comprehensive career focused technology courses for students entering new, complex, and challenging fields in today's technology space.

Covered Topics