Hey Androiders, we are here again quite after a long time to talk about our first “Hello World” Android application with Kotlin. Here we are using Android Studio 3.0.1 to develop out Android Kotlin App. So, before we start I want you to get basic information about kotlin.
Kotlin
Earlier we were using Java to develop Native Android Application in Android Studio. Then Google Announce in Google I/O 2017 that they are going to support Kotlin as “First Class” official language for Android based application development in Android Studio. Kotlin in statically typed programming language which runs on JVM. Designed & developed by JetBrains. We can convert existing java code to kotlin as well and vice versa with no limits. You will get to know more about Kotlin language & how it works later in upcoming tutorials from my side because here after we will be using kotlin language in out upcoming all android related articles.you can start learning from the scratch by Starting Android Development with Android Studio
Start a New Project
So, let’s start creating new android project in android studio. Open android studio & click on Start a new android studio Project.
Create Android Project
We will give a name as Hello World as It’s our first project. In domain name we will give our company name for me here it is The Android Mania with domain which will reflect in out project main package name trailing by application name. & Check Include Android Kotlin support checkbox as we will be using kotlin language for development. Now it can look as below.
Target android Devices
After clicking next to select target Android devices for our application as below.
If you see here I have select Phone and Tablet as supported device which should have minimum android API 21 installed. Which is android 5.0 Lollipop. You can select target APIs as your project needs. For example, if your project is based on basic requirements like some sensors or hardware layer library which is being supported by any low-end device then you can select lower version of SDKs to support maximum android devices available. The lower SDK you select the more devices you can support as simple as that.
Except Phone and Tablet, you can also select Wear, TV, Android Auto or Android Things to develop. You might get to know those types of applications in our upcoming sessions too.
By Clicking next you will see screen to add First Activity and if you don’t want any activity then click on Add No Activity.
Add an Activity to Mobile
You can select activity as per your requirement.
- Basic Activity: A basic Activity which has Menu on top right corner & one Action button with it.
- Bottom Navigation Activity: An activity which has more than one page(by default 3 pages). Which you can navigate from bottom bar buttons like iOS tabbed behaviour.
- Empty Activity: An activity without any extra function like extra button or menu or tab etc. Just a title bar & blank page.
- Fullscreen Activity: An activity which can display it’s content in Fullscreen without action bar & default toolbar in android. Generally this activity is used in Game application.
- Google AdMob Ads Activity: An activity which will display a google AdMob on it. You can select Banner and Interstitial Ad Format.
- Google Maps Activity: An activity which contains Google Map with it you can direct show google map after configure with you application.
- Login Activity: An activity which will have Login screen at first with Username, Password fields & Login Button.
- Master/Details Flow: An activity which will have left bar with page’s title and details area on the right side to display selected title’s result. Generally, it’s being use in Tablet landscape mode.
- Navigation Drawer Activity: This activity will have a navigation menu which can get open by taping on hamburger menu located on top left corner.
- Scrolling Activity: A well designed activity for create a Profile kind of page for example WhatApp Profile section which has Scrolling Toolbar, Menu Bar in Top Right Corner & action button. You can scroll Toolbar & page both in this activity.
- Settings Activity: A single page activity which will provide single lined controls, options or setting which can be saved in Shared Preferences.
- Tabbed Activity: Same as Bottom Navigation activity, this also contains Tabs in activity which will have multiple pages under it which we can change by selecting Tab. In this activity you can choose to have Tabbed Header or swipeable tabs or Spinner for Tabs in action bar.
Configure Activity
After selecting Blank Screen. Click next. It will take you to below screen.
Here I have given MainActivity name as my Activity Class’s Name which will be Kotlin file for Activity. We will mark checked Generate Layout File checkbox to generate the xml designing file for our activity. We will give layout file name as activity_main & check Backwards compatibility to support older devices.
Android Project Structure
Now Click Finish to Create project and let android studio create & build a gradle based android project for you. Once it finishes the create & build project it will dismiss the process dialog & display Android Project structure on left side & opened files on Right pane. Here I will show you how the Left pane looks like. I have expanded all the Android Project files which is being used in Android App Development.
You can open MainActivity.kt file which will look like this.
Here we have just put activity_main layout as setContentView() inside onCreate(). So, when our MainActivity will start it will call onCreate() which will set this layout file as the current view.
Here is the activity_main.xml file look like.
This is having just one RelativeLayout as parent layout which is containing a simple TextView which Text is “Hello World” and it is in centre of its parentview as you can see layout_centerInParent property as true.
Let’s have a look at build.gradle file here which is use to build a project using required libraries, sdk & configurations with gradle support.
Here you can see the kotlin dependency “org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version” which is by default get added when we checked the Support for Kotlin while creating a project.
Here below is the AndroidManifest.xml which known as Heart of Android Application. Because it is most important file of project. Which contains all registrations of All Activities, Broadcast Receivers, Services, Intent filters, Permissions etc…
Now we are all done to run our first Hello World Android Application. To run this, we can connect either our Android Mobile with USB cable/WiFi Network or we can make new Android Emulator to run the Application.
Connect Android Mobile with USB
- Connect your device to your development machine with USB cable. For windows machine you should have already installed USB drivers for your device.
- Enable Developer Option in your phone. (Go To about Phone inside Settings app & Click on Build Number for 7 Times.)
- Go to Setting App and in Device Section Click on Developer Option.
- Scroll Developer Option and enable USB Debugging option.
Create Android Emulator
- In android studio inside Toolbar select the App module & click on Run Button.
- Window will open for Select Deployment Target. Click on Create New Virtual Device.
- Select an appropriate phone inside Select Hardware Session & click on Next.
- You can select the installed API Version for System Image or you can select highest available API version and download the same in that window.
- After clicking Next you can see Android Virtual Device (AVD) screen for AVD setting and click on Finish.
- Once you reach back to Select Deployment Target Screen just click ok.
That’s it. The Android Studio will start AVD and install Hello World App and it will start app automatically. Next time you can select same AVD to run same app again & again after every modification in code.
Now we are all set to learn more about Developing Beautiful android Apps. Stay tuned! See you soon.