Introduction to Kotlin and Android Development Create a Project in Android Studio
Transcript from the "Create a Project in Android Studio" Lesson
>> So now that we have already covered the basics of Kotlin the language, it's time to create our project and start playing with the user interface. So back in Android Studio, I'm going to create a project from scratch. So File > New > New Project. Here, we need to be very specific about the template that we're going to use.
[00:00:24] First, you will see at the left that we can pick the kind of project for Android that we want to create. We can create the project from Phone and Tablet, Wear OS, those are the watches for Android, Android TV, or Auto, that's on Android Auto apps for the car.
[00:00:44] We will focus on Phone and Tablet, and then we need to pick the kind of activity that we want. We don't know what an activity is, yet, but let's say for now, that an activity is one screen in the operating system. So it's asking me what kind of screen or window you want, okay?
[00:01:05] And we will pick one and only one. The one that says Empty Compose Activity because we are going to use Jetpack Compose. And remember that logo that's actually the Jetpack Compose logo. So every time you see that logo, it means Jetpack Compose. All the other templates that we have here are actually for the old, classic way to create user interface, okay?
[00:01:32] So Empty Compose Activity > Next. Then we need a name for the app, I'm going to call this CoffeeMasters. Then it's asking me for a package name. The same happens when you build app for Windows, or for iOS, or for macOS. Every app must have a unique ID within the operating system and within the store, in this case, the Play Store, or any other store.
[00:01:57] So how can we, not guarantee, but how can be safe that we're not going to use a string that someone else is using, as well. Well, one convention that we have agreed on, is to use the name of our website in reverse order. I don't know, for example, com.frontendmasters, and then the name of the app.
[00:02:25] So that's the ID that the unique primary key that will identify our app within the operating system and within the store. You can change that ID until the moment you publish these to the Play Store. If you publish this to the Play Store that ID will still be there forever.
[00:02:46] Because if you change ID it's a different app, so it's not going to be an update. I mean the user is never seeing the ID. The only moment where you're going to see that ID is in the Play Store website. If you look in the Play Store for any app, Microsoft Bing, if you look at the URL, the URL includes, I will zoom in here, the ID of that app.
[00:03:18] This is com.microsoft.bing, okay? That's the only place where users will see your ID. So pick one ID without a show, go without the trademark that you are not allowed to use or something like that. Okay so where are you gonna save this? The language is Kotlin. I cannot pick another one because we are using, or we are going to use Jetpack Compose that is Kotlin only.
[00:03:42] We cannot use Jetpack Compose with Java. And then the minimum SDK, we have already discussed the minimum SDK, I think that for Android 5 is good enough. Finish, and this is going to create the project. We will see that it takes a couple of seconds, sometimes it may take a couple of minutes, actually, to finish, okay?
[00:04:06] It depends on how fast your computer is or how all your Android Studio installation is? Because, if you have installed your Android Studio a few months ago, sometimes you need to download more libraries and it will take some time. If you have just installed Android Studio it will be faster, it should be faster, okay?
[00:04:27] And this is the interface that you're going to see after creating the first project. So I'm going to close MainActivity now, there is a little x, close there. Just to stay clean for a second. First, we have some panels, we have a panel here from the project that you can click, you can open and close the panel.
[00:04:55] In the settings of that panel, you can actually undock it. I mean, if you have multiple monitors, you can undock each panel, the same with an intelligent idea, because it's similar to that ID. But in case you are coming from VS Code or other IDE, it's like a new IDE.
[00:05:13] So I'm jumping quickly over several stuff. And also we have some panels at the right, okay? That you can open or minimize, okay? And then we have, like in the project, we have a project navigator. So you're going to see here, first, there is a folder with something known as a manifest.
[00:05:33] So it says AndroidManifest.xml, okay? That one, so the AndroidManifest.xml is metadata for the operating system. We are not going to touch it for today, probably, but if you wanna customize how your app is working with the operating system, this is probably the place to change metadata. So I promise you, too, that we're not going to use XML, but we're not going to use XML for the user interface.
[00:05:57] But the Android app is still using XML for other things. So then there is a Java folder which is actually funny, okay? Because we are not going to write any Java. But why it's called the Java folder? Because remember our code is compiled into this Dalvik Executable, that is a Java built on machine, even if it's Kotlin, okay?
[00:06:19] So even if it's Kotlin, it goes to the Java folder. Before actually looking at that folder, there are two other folders here that they have a different background color. These are testing projects. So we have two other projects here within our code, androidTest and test. Test is unit testing and androidTest is UI testing, okay?
[00:06:43] We won't cover these today, but have in mind what you have here inside is the test suites, so we can write our own suites and blah, blah, blah. So we have the Java folder where we are going to see our Kotlin code, and here there is a MainActivity.
[00:06:58] Remember, activity for now is kind of a screen, so is the main screen. It's a kt file, so if you open that, that's our code. And you can see on the right that we can select three kinds of editors, Code only, Design only, and Split. You have code and design, and most of the time this is the layout that we are going to use here, okay?
[00:07:27] Split, and we have some code that we will explain in a minute. But before that, I wanna mention that also we have the res folder. This is for resources, okay? So in here we are going to find some things that we will cover later. Here, we put resources, Gradle Scripts.
[00:07:50] Gradle, it's a set of scripts that are being used for the building process. So there is a whole scripting language and platform where you can customize how your app is built. But for now, we will just give them like this. And while it seems like a simple structure, actually, I wanna mention that what you're seeing here is not the real file system, it's what it's called the Android view.
[00:08:19] If you click here where it says Android, I'm talking about there, okay? At the top, instead of Android, I can select Project. And now you will see the real file system that is much more complex. I don't wanna get with this right now, but you will find hundreds of files in your project.
[00:08:41] The Android view is just showing you what you need to see and touch, okay? So in the Java folder we are going to find our source code. That's probably where we are going to play more. Okay, so main activity is, let's say the entry point to my app at this point, okay?
[00:09:07] So there are a lot of code here, but at one point it says Greeting("Android"), okay? I'm going to comment that Greeting("Android"), like that, you comment like in any C language. You can comment with command and the forward slash. And I'm going to say, for example, here, (text: "Hello"), okay?
[00:09:36] Just that, we don't know what everything is here for now, but I'm just adding text here. If you pay attention at the right it says that the preview is out of date. And there is a link there that says, Build & Refresh. You're going to use this a lot, Build & Refresh.
[00:09:55] Unfortunately, the shortcut it's not so simple, okay? I mean, it's not a big deal, but, yeah. At least on the Mac, okay? You need four fingers actually to trigger that link. But actually this is a preview. So the preview will actually preview what you have in the Kotlin code, okay?
[00:10:19] And as you can see they're really really small. I can zoom in, it says Hello Android, okay? Here you have 1:1, that will show you 100%. And this one will actually fit into the screen. Unfortunately, if you have a trackball, the pin Chester, doesn't work here. So you need to use a plus and minus buttons to zoom in or zoom out.
[00:10:44] So we're actually previewing what we have here in the code, okay? Now we will try to understand what's going on. First, is this just a preview? What is a preview? The preview is actually running an emulator behind the scenes. So it's not just an IDE preview. What you see, what you get editor, okay?
[00:11:06] And we think editor is actually executing this in a real Android environment and bringing the canvas back into the ID. That's why every time you make a big change, like I add another text here. It says that the preview is out of date because I need to rebuild and recompile.
[00:11:27] Because this is actually executed in a real Android environment, okay? And they need to Build & Refresh. Some small changes such as the text inside, Okay? Sometimes can be adjusted automatically, and sometimes it's not working, so you need to refresh it. There is a refresh button there at the top, Build Refresh, okay?
[00:11:56] Okay, cool, so.
>> That one is not showing Hello World.
>> It's not showing Hello World, it's showing Hello Android, why is that? So first, here is Greeting("Android"), so let's Build & Refresh. And it's still saying that. If I delete Greeting, it's still showing that. So why is showing a different text?
[00:12:26] That's a pretty good question. Well, it has to do with, what are we previewing? We don't know yet what everything means here, now we will start understanding what's going on. But there is a preview here, there is annotation here that says, @Preview over this code that says, Greeting("Android").
[00:12:51] So it doesn't matter what I'm actually typing here, it does not seem to change the preview. That doesn't mean, that if I run my app, how do I run the app? Using the Play button that you have there, oops sorry. That one or from the Run menu, Run app.
[00:13:12] By default it's going to run your app in an emulator. So if you run this, it will open an emulator, that's the app that we're going to build by the way, that says Hello World. So the preview says Hello Android, but my real Android emulator says Hello World.
[00:13:33] So what you're previewing is what you define as a preview that maybe it's not what is actually being shipped to the app, okay? We will get into the preview idea in a minute. So that means that you can have your own emulator, Android emulator. The emulator is actually built on machine.
[00:13:52] It's running the full operating system. And if you want, you can create more emulators. To do that, you need to use that little phone with the little Android green icon, that's called the AVD Manager or the Device Manager. And the Device Manager will let you create more devices.
[00:14:13] When you install Android Studio you have only one, but you can create other devices. So you can emulate your own Android device. You can have a tablet, a cheap phone, high resolution smartphone, and I can grade to Pixel 5. Next, then I can pick the version of the OS that they wanna emulate.
[00:14:35] Because, remember, every version is different. So by default, you will have the latest version of the API available. Remember the API level, the API number. But if you want, you can download previous version or even future versions. Remember I mentioned that the surnames were our back, so now we have Android Tiramisu, okay?
[00:15:00] That will be API 33. Remember that API number is the most important part for us as developer. But for now, one emulator will make the trick.