Hello World Android Application Using Kotlin


Google officially announced Kotlin as a first class language for Android development at Google I/O 2017. From Android Studio 3.0, Kotlin is included in support for Android Studio. In this post, we will learn Kotlin for the development of an Android application in Android Studio.


To get started, you have to download Android Studio 3.0 or add the Kotlin plugin with your existing Android Studio. To know the basics of Kotlin, click here.

Installing Kotlin Plugin in Android Studio versions earlier than 3.0

In higher versions of Android Studio, we simply create a new project in Android Studio, check the Kotlin support, and start as usual. But for Android Studio with a version less than 3.0, we have to install the Kotlin plugin manually from the Plugins menu.

You can add this plugin by selecting File >> Settings >> Plugins. Then, select or click "Browse Repositories" button. Then, search for Kotlin Plugin in the search box and select the relevant one. Finally, click the "Install" button.



Create Android Project with Kotlin

By default, Android Studio 3.0 has the checkbox for Kotlin Support for your Android Application. Create a new project in Android Studio, check the Kotlin support, and start as usual with Android Studio 3.0. 


For migrating Java Android Project to Kotlin Android Project, you can follow the following process.

Configuring Kotlin

Kotlin support can be configured by selecting Tools >> Kotlin >> Configure Kotlin. Then, Click “Sync Now”. This step is applicable to the Android Studio pre-versions of 3.0. The best way is to update your Android Studio.

In Android Studio 3.0, by default, you have Kotlin Activity. For Android Studio with versions less than 3.0, you can convert their Java Activity into Kotlin Activity.

Open "Quick Search" or press Ctrl + Shift + A (for Windows users) and "Search". Select “Convert Java to Kotlin” or simply press Ctrl + Shift + Alt + K

Coding Part

Now, your MainActivity.kt (converted from MainActivity.java) looks like the following.

  1. package com.androidmads.kotlinsample_helloworld  
  2. import android.support.v7.app.AppCompatActivity  
  3. import android.os.Bundle  
  4. import android.widget.TextView  
  5. import android.widget.Toast  
  6. class MainActivity: AppCompatActivity() {  
  7.     // Declaration  
  8.     var helloTextView: TextView ? = null  
  9.     override fun onCreate(savedInstanceState: Bundle ? ) {  
  10.         super.onCreate(savedInstanceState)  
  11.         setContentView(R.layout.activity_main)  
  12.         // Initialization  
  13.         helloTextView = findViewById(R.id.helloTxtView) as TextView  
  14.         // Assigning data to TextView in runtime  
  15.         helloTextView!!.text = "Hello Kotlin!";  
  16.         // Click Listener in Kotlin  
  17.         helloTextView!!.setOnClickListener({  
  18.             Toast.makeText(applicationContext, "Hello TextView Clicked", Toast.LENGTH_LONG).show();  
  19.         })  
  20.     }  
  21. }  

Extends in Java is replaced by colon (:). To know more, click here.

Kotlin is a new programming language for Android development. Even though Kotlin is announced as a first class language for Android, Java will not be deprecated from Android development. Please tell me what you think about this tutorial. Also, do let me know your views about the future of Java in Android development.

Similar Articles