Using Kotlin in Android Studio 3.0 (Part 1)

综合技术 2018-03-07

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

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:
  • 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:
    My First Kotlin App
    Hello Kotlin
  • 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:
  • 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.

DZone

责编内容by:DZone (源链)。感谢您的支持!

您可能感兴趣的

Android Studio – 第四十六期 不会ANR的轮播写法... 最近在学习撸撸的代码,发现他的轮播写法很独特,但是有bug,就重新修改了一下,现在支持左右点击和圆点自定义。 BannerAdapter:(注意适配器自己看...
12 reasons why you should start using Kotlin for A... We’ve reached the end! The rest of articles were very practical, but today I wan...
Easy Version Control With Git in Android Studio As you know, it's best practice to always use source control management (SCM) fo...
Android app does not appear in Android Studio Emul... I am very new to Android development. I have tried to recreate an app where i...
直接添加 Android Studio 工程到 Framework 下之 Makefile 指定 M... Android 系统源码下的 App 工程目录结构还是跟 Eclipse 时代一样,项目根目录下直接包含 src、res、AndroidManifest.xml...