Kotlin Android Extensions
Tags: android, kotlin, productivity, beginners
Published: 2017-10-06 00:12:01

---
title: Kotlin Android Extensions
published: true
description: If you thought Butter Knife was good, you will be dazzled by Kotlin Android Extensions
tags: #android #kotlin #productivity #beginners
cover_image: https://thepracticaldev.s3.amazonaws.com/i/bokw1yei8g3zr7ecasm1.jpg
---

##Kotlin Extensions

I wrote a post on [Butter Knife](https://dev.to/lemuelcco/intro-to-butter-knife-for-android-views-d4e) 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

<img src="https://thepracticaldev.s3.amazonaws.com/i/u2adyz6yb4lxzownw0kv.PNG" alt="Mountain View" style="width:304px;height:228px;">
</br>

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
<?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>
 
```
<br>
<br>
You need to configure your Kotlin as usual 

<img src="https://thepracticaldev.s3.amazonaws.com/i/u15iub07srjqdsfxzdsw.PNG" alt="Mountain View" style="width:304px;height:228px;">
</br>

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

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

```

<br>
<br>

##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:


```java

apply plugin: 'kotlin-android-extensions'

```

##Import synthetic properties

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

```java
import kotlinx.android.synthetic.main.<layout>.*
```

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

```java
import kotlinx.android.synthetic.main.activity_kotlin.* 
```

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:

```java

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())
        }
    }
}


```
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.