An Introduction to Kotlin for Android Developers

How to Get Started

Android Studio 3.0

Dependencies

maven { url 'https://maven.google.com' }
buildscript { ext.kotlin_version = '1.1.2-4'//note use the latest version available .... }
dependencies { //note, be sure to reference what you defined as your version name classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" }
apply plugin: 'kotlin-android'
//we are referencing the same version name defined in the project build file compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

Creating Kotlin Classes and Activities

Kotlin Developer Basics

Null Safety

var testVar : String = "a test string" testVar = null //error, won't compile
var testVar : String? = "a test string" testVar = null //no issue.
val testVal : String? = "a test string" val testValCount : Int = testVal.length //wont compile as testVal may be null!
val testVal : String? = "a test string" val testValCount : Int? = testVal?.length //compiles, but testValCount may be null! val testCalc : Int = testValCount * 2 //wont compile....testValCount may be null
val testVal : String? = "a test string" val testValCount : Int = testVal?.length ?: 0//defaults to 0 in the event that testVal is null val testCalc : Int = testValCount * 2 //this works!
val testVal : String? = "a test string" val testValCount : Int = testVal!!.length//compiles but may thrown a NPE

The Static Modifier

class PracticeArea() { companion object { val staticFun = "STATIC FUN" } }
Log.i("TAG", PracticeArea.staticFun)

Access/Visibility Modifiers

Methods are Final

protected fun onViewReceived(){ //is set to final } protected open fun onViewReceived(){ //children can override }

What does Any mean?

Anonymous Inner Classes

//can be replaced with lambda, but shown for demo purposes view.setOnClickListener(object : View.OnClickListener { 
override fun onClick(v: View?) { //do your click stuff... }
})

Android Practices in Kotlin

Parcelables

data class PracticeArea(val name: String, val imageLink: String,
val subTitle: String, val body: String,
val serviceOfferings: ArrayList<ServiceOffering>) : Parcelable {

/**
* Companion object is used to defined static members to the class..
* in our case the creator.
*/

companion object {
/**
* Parcelable creator.
*
* @JvmField used make the creator implementation visible as a field to Java.
*
*/

@JvmField
val CREATOR = object : Parcelable.Creator<PracticeArea> {
override fun createFromParcel(source: Parcel): PracticeArea? = PracticeArea(source)
override fun newArray(size: Int): Array<out PracticeArea?> = arrayOfNulls(size)
}
}

/**
* Secondary constructor for the parcelable
*/


protected constructor(parcelIn: Parcel) : this(parcelIn.readString(), parcelIn.readString(),
parcelIn.readString(), parcelIn.readString(), parcelIn.createTypedArrayList(ServiceOffering.CREATOR))


override fun writeToParcel(dest: Parcel?, flags: Int) {
dest?.writeString(name)
dest?.writeString(imageLink)
dest?.writeString(subTitle)
dest?.writeString(body)
dest?.writeTypedList(serviceOfferings)
}

override fun describeContents() = 0
}

Starting an Activity

//Java Intent mIntent = new Intent(context, DestinationActivity.class); //Kotlin val intent = Intent(context, DestinationActivity::class.java);

findViewById No More

import kotlinx.android.synthetic.main.activity_list.*
import kotlinx.android.synthetic.main.list_view_holder.view.*
fun bindView(area: PracticeArea?, practiceListener: PracticeListener) {
itemView.listImage.setImageURI(area?.imageLink)
itemView.listTitle.text = area?.name
itemView.listBody.text = area?.body
itemView.listSubTitle.text = area?.subTitle

......
}
apply plugin: 'kotlin-android-extensions'

Adapters, RxJava, & More

Road Bumps

Closing

--

--

--

Mobile Lead & Architect @ CapTech. I specialize in delivering products for Fortune 500 clients in Mobile, Android Architecture, & IoT

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Golden — what activities do I have to do to get an airdrop?

JSON/HTTP transcoding to gRPC using Envoy

Airflow: In a Time where Timing is of the Essence — Part I

My Essential LoFi Music

From a Newbie to a Command Line Master

Python 3.9 New Features & How to Use Them

Interview Series by TopAgency

Using final variable in lambda with construction initialization

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Clinton Teegarden

Clinton Teegarden

Mobile Lead & Architect @ CapTech. I specialize in delivering products for Fortune 500 clients in Mobile, Android Architecture, & IoT

More from Medium

Uses of Coroutines in Android

Security in Android 12 — For Developers

RecycleView Kotlin — Android Studio

Defining server response model using Sealed Class