The Ultimate Guide to Kotlin Programming

Kotlin is a statically typed programming language for modern multi-platform applications. Kotlin’s syntax is easy to read and it has powerful features which makes software development easier. 

This article aims to help Android developers learn the basics of Kotlin programming by showing how to set up a Kotlin Android project in Eclipse environment and explaining the basic aspects of the language.

1. Setting up The Environment:

The latest version of Android Studio includes support for native development using Kotlin, so you don’t need to install anything else if you are using that IDE. In this article we will be using Eclipse IDE, but all steps should be similar enough for other IDEs as well. To get started let’s download IntelliJ IDEA Community Edition, which has Kotlin support built in.

2. Creating a Project:

Once the tool is installed open it up and create a new project (File -> New Project). We will be creating a simple application that shows Hello World in the main activity. The next screen asks for the project name and location so let’s call it “Kotlin Android Demo” and select “Android ICS 4.0 – API Level 14 (or higher)” as Target SDK (this should already be pre-selected). If you don’t have Google APIs included in your Android SDK then you can download them later from Android Studio Preferences -> Appearance & Behavior -> System Settings -> Android SDK Manager. We won’t need to use any third party libraries for this example, but if you want to use any then check the appropriate section of the documentation.

3. The Basic Kotlin Classes:

As mentioned before our first demo application will show Hello World on screen so we could start by writing an activity that extends android app. Activity class and overrides it’s on Create method.

Note that the first line of Kotlin class keyword declares the new class, followed by its name and then base classes separated by a colon (:). The constructor is not part of the declaration so it should be added to the body of the class as shown above. We are also calling a special method for creating an instance called ‘make’ which is provided by Kotlin Android Extensions plugin for IDEA so don’t worry about it being defined somewhere else (it’s not a standard part of the language).

Note: ‘this’ in Kotlin can be used to reference current object so we don’t need to explicitly pass it as a parameter. This is similar to what we would do when creating an Activity instance in Java code; except for this time we are using Kotlin syntax which looks much simpler and cleaner. That’s one of the main things that make writing Android applications with Kotlin great – all those things that you usually need extra libraries for (annotations, generics) just work out-of-the-box and create more readable and concise code. Also, no more typing ‘super.’ everywhere!

As you will probably notice there is no semicolon at the end of each statement in Kotlin, just like in Python. To know more check RemoteDBA.com.

4. R.layout.main:

In Kotlin instead of ‘findViewById(R..’ we have to use the special resource identifier called R which is a type of Int and can’t be used for anything else so it will only conflict with other identifiers if you try to name a method or class the same as R . It works pretty much the same way though so this is how would call our main layout from code – by using its identifier declared in Android’s XML file (located in app -> res -> layout -> main.xml). In this case we set its content view to current activity’s layout, which is actually created from that XML file, and then show a Toast message with ‘Hello world!’ text.

5. The Main Layout:

The layout is pretty much standard for this simple example however there are some things that you should know about in case you’re not familiar with them yet (like I was when I started playing with Android). First of all, the top bar (app -> res -> values -> strings.xml) contains string resources which can be called from our code by using R.string.’text_name’. It’s used to create labels and messages so let’s change our main layout file to look like this:

Conclusion:

As you can see writing Android apps in Kotlin is very easy and it has way less boilerplate code than Java does. There are some limitations to it right now, like the fact that you cannot use any Java class unless it’s already part of Android SDK (although most popular libraries have Kotlin counterparts) which might be a problem if you want to integrate some 3rd party library or something similar. I believe though that once Google starts officially supporting Kotlin for Android development this limitation will be removed as well as many others.

Leave a Comment