
Introduction
In this article, we will learn how to create and use extensions for Android development using Kotlin. We have read about Kotlin for Android development. If you are new to Kotlin, read my previous article on Kotlin:
Extensions
Kotlin, similar to C# and Gosu, provides the ability to extend a class with new functionality without having to inherit from the class or use any type of design pattern such as Decorator. This is done via special declarations called extensions.
Coding Part
I have detailed the article as in the following steps.
- Step 1 - Creating a new project with Kotlin
- Step 2 - Creating Extensions in Kotlin
- Step 3 - Implementation of Extension in Kotlin
Step 1 - Creating a new project with Kotlin
- Open Android Studio and select "Create new project".
- Name the project as per your wish and tick the Kotlin Support checkbox.
- Then, select your Activity type (For Example - Navigation Drawer Activity, Empty Activity, etc.).

- Click the "Finish" button to create a new project in Android Studio.
Step 2 - Creating Extensions in Kotlin
In this part, we will see how to create an extension for Kotlin.
- To declare an extension function, we need to prefix its name with a receiver type, i.e. the type being extended. The following adds a swap function to MutableList<Int>.
- fun MutableList<Int>.swap(index1: Int, index2: Int) {
- val tmp = this[index1] // 'this' corresponds to the list
- this[index1] = this[index2]
- this[index2] = tmp
- }
- We will take an example to show Toast with Short duration. Traditionally, the Toast is shown like below.
- Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
- We can simplify this by the using extensions in Kotlin.
- Create a Kotlin file named as “Extensions.kt”. Then, add the following code part.
Here, the base or receiver type of toast is “Context”. So, we have created a function prefixed with “Context”.- fun Context.showShortToast(message: CharSequence) {
- Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
- }
- In the same way, I have created an extension to show “Toast with Long duration” and “Alert Dialog with the single option”. You can find the full code below.
- package com.androidmad.ktextensions
- import android.content.Context
- import android.support.v7.app.AlertDialog
- import android.support.v7.app.AppCompatActivity
- import android.view.View
- import android.widget.Toast
- fun Context.showShortToast(message: CharSequence) {
- Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
- }
- fun Context.showLongToast(message: CharSequence) {
- Toast.makeText(this, message, Toast.LENGTH_LONG).show()
- }
- fun AppCompatActivity.showAlert(title: CharSequence, message: CharSequence) {
- val builder = AlertDialog.Builder(this)
- .setTitle(title)
- .setMessage(message)
- .setPositiveButton("Ok", { dialog, which ->
- })
- val dialog: AlertDialog = builder.create()
- dialog.show()
- }
Step 3 - Implementation of Extension in Kotlin
In this section, we will see how to call the above-created extension method in Android.


Join the conversation! Your thoughts help the community grow.