DEV Community

Cover image for Kotlin Android Extensions
Lemuel Ogbunude
Lemuel Ogbunude

Posted on • Updated on

Kotlin Android Extensions

Kotlin Extensions

I wrote a post on Butter Knife and it's beautiful features that reduce boilerplate code. If you thought that was impressive you will be even more dazzled by Kotlin Android Extensions. Kotlin Android Extensions enable us get the same result as using multiple findViewById() functions without writing any extra code.

So I will be using a basic Android app that adds two numbers and displays the answer. The aim here is not to demonstrate the functionality but rather the ease of work done with little code.

The UI

Mountain View

So above we have a button to add the two digits inputted by the user, the answer is displayed using the text view below the button.

I will display the XML code for clarity, so that the IDs of the views can be noted, we will be using those later.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context="com.example.charl.kotlinadd.KotlinActivity">

    <EditText
        android:id="@+id/firstDigitEdit_Txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="91dp"
        android:ems="10"
        android:inputType="number" />

    <EditText
        android:id="@+id/secondDigitEdit_Txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/firstDigitEdit_Txt"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="33dp"
        android:ems="10"
        android:inputType="number" />

    <Button
        android:id="@+id/addBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/secondDigitEdit_Txt"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="38dp"
        android:text="ADD" />

    <TextView
        android:id="@+id/answerTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/addBtn"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="49dp"
        android:hint="0"
        android:textSize="30dp" />

</RelativeLayout>

Enter fullscreen mode Exit fullscreen mode





You need to configure your Kotlin as usual

Mountain View

You should notice these plugins and some other changes automatically in your module's build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

Enter fullscreen mode Exit fullscreen mode

Begin using Android extensions

In order to begin, all you need is to enable the Android Extensions Gradle plugin in your module's build.gradle file:


apply plugin: 'kotlin-android-extensions'

Enter fullscreen mode Exit fullscreen mode

Import synthetic properties

Though this is normally done automatically by the compiler once you start coding, you could also do it by yourself:

import kotlinx.android.synthetic.main.<layout>.*
Enter fullscreen mode Exit fullscreen mode

The layout depends on the file name of your Kotlin layout file, for example if mine is activity_kotlin I am importing:

import kotlinx.android.synthetic.main.activity_kotlin.* 
Enter fullscreen mode Exit fullscreen mode

So the main code is below, all you need to know is actually the IDs of the views, that is basically what I used to access the views:


class KotlinActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kotlin)

        addBtn.setOnClickListener {

/*get value from first text*/
            val A = Integer.parseInt(firstDigitEdit_Txt.text.toString())

/*get value from secondtext*/
            val B = Integer.parseInt(secondDigitEdit_Txt.text.toString())

/*sum it up*/
            val C=A+B

/*display using text ID*/
            answerTxt.setText(C.toString())
        }
    }
}


Enter fullscreen mode Exit fullscreen mode

So there you have it, if I were to use findViewById(), it would have drastically increased the lines of code written.
Kotlin is really good and moving on very strong, Java is still very well in the game, it ain't dead, but I must say that Kotlin is really beautiful.

Top comments (0)