Using Kotlin in Android Studio 3.0 (Part 1)

移动开发 DZone (源链)


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 (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:
  • 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.os.Bundle  
class MainActivity : AppCompatActivity() {        
override fun onCreate(savedInstanceState: Bundle?) {          
     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.


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稿源:DZone (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Using Kotlin in Android Studio 3.0 (Part 1)

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录