Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using Kotlin in Android Studio 3.0 (Part 1)

DZone's Guide to

Using Kotlin in Android Studio 3.0 (Part 1)

This quick tutorial will show you how to get started developing mobile apps for Android with the Kotlin language.

· Mobile Zone ·
Free Resource

Background

When I wrote this article, I assumed that you already know about Android programming before. If you are Android beginners, you can read my other article, “Introduction Android programming using Android Studio 3.0.”

Why Is Kotlin?

  • Kotlin is very intuitive and easy to learn for Java developers
  • Android Studio 3.0 can understand, compile, and run Kotlin code easily

Installing the Android Studio 3.0 and Kotlin Plugin

  • We must download and install Android Studio 3.0 at  developer.android.com (current version is 3.0.1)
  • Checking Kotlin plugin installed by selecting File > Settings

Image title

Creating a New Android Application Using Kotlin

  • Creating a new project by selecting File > New > New Project. Enter a text for Application name and check out Include Kotlin Support checkbox as the following screenshot:

Image title

  • Click Next and Next and select Empty Activity. Click Next and Finish.
  • So far, we have activity_main.xml file (includes two modes: Design and Text) and MainActivity.kt file.
  • In right window, select app > res > values > strings.xml. Add a string element name hello_world to strings.xml file as follows:
<resources>
    <string name="app_name">My First Kotlin App</string>
    <string name="hello_world">Hello Kotlin</string>
</resources>
  • Select activity_main.xml file in Design mode. Select TextView and set a value (message) for ID and set a value (@string/hello_world) for text in Attributes window:

Image title

  • Select MainActivity.kt file and adding the import as follows:
import kotlinx.android.synthetic.main.activity_main.*
  • Add the following code line to the onCreate method:
message.text = "Hello My Kotlin App in Android Studio 3.0"


  • Do you remember the message? It is ID of TextView.
  • The complete code of MainActivity.kt, so far, can look like:
package com.example.admin.myfirstkotlinapp    
import android.support.v7.app.AppCompatActivity  
import android.os.Bundle  
import kotlinx.android.synthetic.main.activity_main.*  
class MainActivity : AppCompatActivity() {        
override fun onCreate(savedInstanceState: Bundle?) {          
     super.onCreate(savedInstanceState)          
     setContentView(R.layout.activity_main)          
     message.text = "Hello My Kotlin App in Android Studio 3.0"      
     }  }
  • Run App and the message "Hello My Kotlin App in Android Studio 3.0" will display.

Conclusion

In this article, I have only introduced how to install and create a simple Android application using Kotlin in Android Studio 3.0. In my next articles, I will introduce about more Kotlin language and how to create more complicate Android applications.

Topics:
android ,mobile ,mobile app development ,kotlin ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}