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阅读原文】。感谢您的支持!

您可能感兴趣的

AndroidStudio 配置 AspectJ 环境实现AOP 昨天看了一段android配置aspectj实现AOP的直播视频,就试着自己配置了一下,可能是因为我自己的AndroidStudio环境的问题,碰到了不少的坑(其实还是因为对gradle理解的不多),但总归是配置好了,就分享一下。...
Gradle Kotlin DSL: First Impressions As an learning exercise, now that Gradle has a  Kotlin  DSL, I decided to migrate a Gradle build f...
Error Using Map Library Can not find a possible runtime error using maps I am getting a segmentation fault somewhere, but ...
AndroidStudio离线配置gradle.导入工程等问题剖析... 问题 导入工程,不断的building 卡死 因为长城防火墙,我们无法连接到google网站,导致有些资源无法下载。AndroidStudio这种自动化的开发利器,显得没那么强大。我们想离线搭建Android开发环境变的...
利用Android Studio运行第一个flutter程序 所需工具 Flutter SDK Android SDK Android Studio 详细步骤 先确保你已经安装好Android Studio且配置好安卓开发环境了。 安装Flutter插件 ...