Android Studio 3.0 with Kotlin: First Look

The most awaiting release, Android Studio 3.0 has been released a few days back with exciting features including Kotlin and Java 8 support.  Even though developers were able to use Kotlin and Java 8 earlier, this time they do not need any extra configurations to enable them. This article explains how to create a hello world Android application using Kotlin.


Wait... This is Java Helps but I am writing about another language! Am I running out of content? Of course Kotlin is a new language but still, it is running on top of Java Virtual Machine. Kotlin can be compared with Groovy in terms of how it compiles the code. Both of them produce the same binary code that every Java developer is familiar with. Therefore Kotlin is another baby joining the family of JVM languages but with different syntax and more features.

Either download Android Studio 3.0 or upgrade your existing Android Studio to 3.0 before reading further.

Step 1:
Create a new Android Studio project and name it 'Hello World.


When you create the project, tick the "Include Kotlin support" checkbox.


Step 2:
Click all upcoming 'Next' and the last 'Finish' buttons with default values.

Step 3:
Now you should get the MainActivity.kt as shown below. Note the file extension. It is not MainActivity.java but MainActivity.kt.


Step 4:
Run the application and make sure that everything works before we play with Kotlin.

Step 5:
JetBrains has a decent documentation on Kotlin. I highly recommend you to go through it. At least, going through the basic syntax will be helpful to understand the code.

Package and import statements are similar to the Java package and import statements. The MainActivity class is defined as a sub-class of AppCompatActivity. The onCreate function is equivalent to the onCreate method of Java version but the syntax is completely different. The onCreate method receives a nullable (because of that ? at the end of Bundle) savedInstanceState object. The rest is similar to Java implementation of MainActivity class.

Step 6:
Modify the activity_main.xml as shown below. This code creates an EditText and a Button.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.javahelps.helloworld.MainActivity">

    <Button
        android:id="@+id/btnSayHello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Say Hello" />

    <EditText
        android:id="@+id/etName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/btnSayHello"
        android:layout_centerHorizontal="true"
        android:ems="10"
        tools:layout_editor_absoluteX="71dp"
        tools:layout_editor_absoluteY="151dp" />

</RelativeLayout>

Step 7:
Modify the MainActivity.kt as given below. This code finds the etName and btnSayHello components and set the onclick listener to the btnSayHello.
package com.javahelps.helloworld

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Button
import android.widget.EditText
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Find the EditText etName
        var etName = findViewById<EditText>(R.id.etName)
        // Find the Button btnSayHello
        var btnSayHello = findViewById<Button>(R.id.btnSayHello);

        // Set onclick listener
        btnSayHello.setOnClickListener {
            Toast.makeText(this, "Hello ${etName.text}", Toast.LENGTH_LONG).show();
        }
    }
}
I am not going to explain the Kotlin specific syntaxes here. Instead, detailed articles I found in the Internet have been shared for your information.

Step 8:
Run the application and check the output.


In conclusion, I find Kotlin interesting due to its modern appearance. The ability to use exsiting Java libraries and the IDE support provided by IntelliJ makes it a reliable alternative to Java. Kotlin also has its drawbacks but since Google has decided to go with Kotlin, I would advise Android beginners to use Kotlin. Java developers can easily adapt to Kotlin eventhough still they can use Java to develop Android applictions.

Find the project @ GitHub.
Previous
Next Post »

Contact Form

Name

Email *

Message *