Kotlin integrations with Android SDK (KAD 05)
Kotlin not only simplifies the code you create on this language, but also the Java code you use from Kotlin.
How does this work? It simply uses some generic structures and give them a more “Kotlin” feel.
In this article, you’ll see some examples, and how it improves code homogeneity. It also prevents part of the boilerplate involved in Java code.
Kotlin integrations with Android SDK
As Android framework is basically a Java library, everything we mentioned before about Java applies perfectly to Android.
Let’s see some examples
getters and setters are mapped to properties
We’ve already seen this in previous articles. This is the explanation I promised.
As we talked, Kotlin uses properties instead of fields + getters + setters, and the way to assign values and get them is the same we’d use in a public field in Java.
But as we know, there’s a code being executed that can be a simple assignment, or the custom operation we implement.
Want to learn Kotlin?
Check my free guide to create your first project in 15 minutes!
The rule here is that any setX
and getX
found in Java will be mapped to an x
in Kotlin.
For instance, if you are using a TextView
, you can set the text using a property-like sentence:
12 | val textView:TextView=...textView.text="My Text" |
And this applies to any classes, of course. You can access to applicationContext or layoutInflater
in an Activity (for example) the same way:
12 | layoutInflater.inflate(R.layout.view_item,parent)val hello=applicationContext.getString(R.string.hello) |
If you are worried about performance, in fact these this will keep mapping to the original getters and setters, so it’s the same as calling them directly.
Interfaces with one method are mapped to lambdas
This is awesome. Aren’t you tired of creating anonymous classes for any simple things in Android? With Kotlin, you can prevent doing this for many Android listeners.
The condition is this: it needs to be an interface with a single method. The more typical example is setOnClickListener
from View
class.
You can do something as simple as this:
1 | view.setOnClickListener{navigateToNextActivity()} |
Saw how easy?
If you need the view that is returned from the original interface, you can also recover it::
1 | view.setOnClickListener{v->navigateWithView(v)} |
Spoiler: What’s this weird structure? It’s called lambda, and it’s a way to represent functions where the input parameters are defined at the left side of the arrow. The right side of the arrow is the body of the function, and the last line is the result. We’ll see some about it in future articles.
Conclusion
It’s that easy to integrate other Java libraries, and the Android framework in particular, in Kotlin projects. The good part is that it also looks like Kotlin, which is helpful both because the code is simpler and because all your code will feel like Kotlin.
If you want to learn more about Kotlin, join the list and you’ll receive them right in your inbox.
I’m in love with Kotlin. I’ve been learning about it for a couple of years, applying it to Android and digesting all this knowledge so that you can learn it with no effort.
SharesLike this:
Loading...Related
相關推薦
Kotlin integrations with Android SDK (KAD 05)
Kotlin not only simplifies the code you create on this language, but also the Java code you use from Kotlin. How does this work? It simply uses some
Extension functions in Kotlin: Extend the Android Framework (KAD 08)
Extension functions are a really cool feature that Kotlin provides, and that you’ll find yourself using a lot when writing Android Apps. We have to a
【已解決】mac上appium報錯:“Could not find aapt Please set the ANDROID_HOME environment variable with the Android SDK root directory path”
resource sset root could not fun ror 環境 apt direct 按照網上教程配置完appium環境後,真機跑自動化過程,遇到如下報錯: appium報錯如下: [ADB] Checking whether aapt is present
C:\Users\zhen\AppData\Local\Android\Sdk\platform-tools\adb'' finished with non-zero exit value 1
Android studio編譯時報錯 Process 'command 'C:\Users\zhen\AppData\Local\Android\Sdk\platform-tools\adb'' finished with non-zero exit value 1 只需要在setting
android-sdk-windows\build-tools\26.0.0\aapt.exe'' finished with non-zero exit value 1
android-sdk-windows\build-tools\26.0.0\aapt.exe’’ finished with non-zero exit value 1 沒改動程式碼,專案編譯的時候報了這個錯誤 com.android.ide.common.p
[Android] SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment
從網路上下載了一個範例,開啟時會顯示這個錯誤: I recently tried to import sample Android games I downloaded from Google’s developer website. After importing them into Android St
Classes in Kotlin: More power with less effort (KAD 03)
Classes in Kotlin are as simple as possible so that you can express the maximum amount of logic with the less code possible. I’ll show quickly how yo
Variables in Kotlin, differences with Java. var vs val (KAD 02)
In this second chapter we will see how variables work in Kotlin, what is val and var , and when to use one or the other. I wanted to start from he
Lambdas in Kotlin, and how they simplify Android development (KAD 07)
Lambdas are one of the most powerful tools in Kotlin, and in any other modern language, since it allows modelling functions in a much simpler way. Th
Build an application with the Watson Android SDK in Android Studio
IBM Watson may be best known for competing on the US quiz show Jeopardy, but the technology goes far beyond simple questions and answ
Android SDK安裝時出錯“android Failed to rename directory”的解決方法
add size android-s 目錄 解決 androi director lpack lis Android SDK安裝時出錯“android Failed to rename directory”的解決的方法 安裝Android SDK時遇到Faile
關於MacBook怎麽更新Android SDK
ack span fontsize mod 更新sdk -m 無法更新 style jsb 昨天公司的人給了我一個VPN,可是還是無法更新SDK,後來發現將下圖:通過VPN發送全部流量勾選以後就能夠連接更新了,哎。處處皆學問,特此分享一下此經驗。喜歡的朋友關註我哦!多謝支
[轉]Android Studio啟動時出現unable to access android sdk add-on list
上大 ras server tails dea tar ext ida 錯誤信息 轉載請標明出處:http://blog.csdn.net/xx326664162/article/details/50563122 文章出自:薛瑄的博客 你也可以查看我的其他同類文章,也會讓你
受不了Android SDK文檔打開緩慢問題,自己開發簡易脫機瀏覽器。
write 瀏覽歷史 code int protected ppt 便在 1.0 bundle google android sdk離線文檔打開的時候特別慢,據說是要從谷歌官網拉取一些東西導致的。脫機瀏覽能夠解決該問題。PC端能夠使用firefox。 可是Android
Android SDK Manager僅有一個版本的問題
google devtools 多版本 blog system ges 虛擬 eclipse 記事本 搭建好MonkeyRunner的環境之後,建立虛擬器的時候發現SDK的管理器中只有4.3的版本,查閱了一下百度,問題解決如下: (1)在c:\Windows\System3
android sdk 鏡象網站
popu tools 相關 ons art ont clas 安裝 net 因為一些原因。Google相關非常多服務都無法訪問,所以在非常多時候我們SDK也無法升級,當然通過技術手段肯定能夠解決,可是比較麻煩,並且下載速度也不怎麽樣。這裏筆者介紹一個國內的Android
Android SDK代理服務器解決國內不能更新下載問題
b2c 新的 jai 但是 con bit 訪問 com 端口 國內高校的開源鏡像站 中國科學技術大學(debian.ustc.edu.cn) 上海交通大學(ftp.stju.edu.cn) 大連理工大學(mirror.dlut.edu.cn) 北京交通大學(mirror
[轉]關於sdk更新Android SDK Tools 25.3.1版本後使用sdk manager閃退
art 運行 ted studio 令行 最新 pro vdma log 昨天這兩個manager還工作正常,今天更新了一下,發現不可用了,運行avd manager和sdk manager沒反應,搜了好多文章,然後看到了下這篇文章《關於sdk更新Android SDK T
Android SDK 綁定源代碼
options ces box imp view ews onf art for 方法: 1. Windows 修改文件 C:\Users\Administrator\.AndroidStudio2.3\config\options\jdk.table.xml
關於android SDK安裝Failed to fetch URL http://dl-ssl.google.com/android/repository/addons_list-1.xml出錯二
想要 www size lan reload repo 來吧 img details 近日,由於某些原因,大家在使用Android SDK升級的時候。出現了訪問Failed to fetch URL http://dl-ssl.google.co