Kotlin Android Tutorial Shorten findViewById setOnClickListener Basic Kotlin Syntax

Welcome to Kotlin Android tutorial.

Recently, Google announce official support to Kotlin as programming language for Android application development.

This Kotlin Android tutorial will guide you some basic and important syntax of Kotlin.

Kotlin has some unique features which can help us to shorten the classic Java source code.

Kotlin Android tutorial will teach how you can shorten findViewById and setOnClickListener.

You will also learn function’s syntax of Kotlin in this Kotlin Android tutorial.

First, check the output Kotlin Android tutorial then we will develop it.

Download Source Code for Kotlin Android tutorial

Try Our App

Download DemoNuts app to learn anytime, anywhere: Click to get demonuts app

Or search “demonuts” in play store from android mobile device.

How to get maximum benefit from Kotlin Android tutorial

Click to get maximum benefit

Creating Kotlin Android tutorial example

If you are using adblocker, then you will not be able to show our site smoothly. For example, you find step 6 after step 1 or similar to this. So please disable adblocker to fully utilize all resources of DemoNuts

Step 1: Create an Android Studio project with Kotlin support.

If you are not using Android Studio 3.0 or above version then you need to upgrade your Android Studio.

I have downloaded Android Studio 3.0 version (canary version) from this link.

Follow steps in this link to run 3.0 version.

When you are creating new project, include kotlin support as per below image.

Kotlin Android tutorial

Include Kotlin Support

Step 2: Updating activity_main.xml file

Add below source code into activity_main.xml 

Step 3: Updating MainActivity.kt

Copy following source code into MainActivity.kt class

Step 4: Description of Various Syntaxes

Removing findViewById

Till now you are setting ids to views by findViewById. But you need to use findViewById  for every single view in whole Java file. In Kotlin, you can say good bye to findViewById. But How? 

First, put following function

Now to give ids to every views, you just need to do is

Give id at the last and view type(Button, ImageView, TextView etc.) at middle as shown in above code.

Declaring Variables

Define variables in Kotlin as follow

String variable

Integer variable

Now take attention at above two lines. First line starts with var and second with val.

There are two types of variables in Kotlin. Mutable and Immutable.

 var vs val in Kotlin

var is mutable variable, that means value of this type of variable can be changed.(Read and Write)

val is immutable variable, that means value of this type of variable can not be changed. (Read only)

Setting Button’s label or text

conventional method setText() is not used here.

Giving button’s text to String Variable

Setting TextView’s text from String Variable

Giving value to integer variable

No need of semicolon(;) at the end.

Reducing lines for setOnClickListener

Syntax for function

As you can see there is little difference than Java code. In Java, we used to give return type at starting where here it is written at the end of first line.

Void in Java is written as Unit in Kotlin. For example

Setting Image Resource

How to get exact Android tutorial while searching in Google

Click to get Important Google searching tips

End of Kotlin Android tutorial. Comment your queries and reviews in comment section below.

If you find this example useful, then SHARE it using various sharing options at left panel.

Thank you 🙂

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

2 Responses

  1. didi says:

    Hello Hardik, nice article. I wanna ask about bind function. Is that not make process slower? may be you can explain to me about that. Sorry i’am an newbie 😀 thanks

Leave a Reply

Your email address will not be published. Required fields are marked *