Open In App

7 Kotlin Extensions That Every Android Developer Should Know

Improve
Improve
Like Article
Like
Save
Share
Report

Kotlin is a programming language designed specifically for Android and JVM i.e. Java Virtual Machine. The language is quite popular among developers and you need to know that Google has already declared Kotlin as the preferred language for Android application development. There are various Kotlin extensions also available that empower Android developers and programmers by making Android development very much easier and convenient for them. Moreover, these extensions are smart enough in terms of collection filtering, lazy-loading means loading only those resources which are essential to load for better memory management), etc.

7-Kotlin-Extension-that-every-android-developer-should-know

You must now be curious to dive deeper into the properties those useful Kotlin extensions possess for speeding up the Android development process without making any compromises on accuracy and flexibility. Are you still doubting the potential of these readily available, royalty-free extensions? Let’s know in detail about the top Kotlin extensions with which every Android developer or programmer should be aware:

1. Android KTX

Android KTX is known amongst the community members of Android Development as a collection of Kotlin extensions rather than a single one. Whether it is about committing changes related to Kotlin coroutines, lambdas, or named parameters passed in high-order Kotlin functions, all this is achievable as soon as you get yourself started with this concise pack of Kotlin extensions. 

Indeed, each module of Android KTX is organized and equipped with more than one package whose syntax is helpful in managing the versioning regardless of fulfillment of dependency-declaration requirements. All you need to do now is add the dependency repositories 

dependencies {

   implementation ‘androidx.core:core-ktx:1.0.1’

}

to the Kotlin project file saved in the format build.gradle. 

2. Firebase

Firebase is that Kotlin extension that can for sure change the future of Android development plus the management of experience of users actively taking interest in Android apps. And here comes the good news – the beta version of Firebase is out in the market and it welcomes you with newer features whose bugs, if emerge, can be rectified after you report them. Also, there is no need of taking stress regarding the safety of your smartphones or desktops onto which Firebase is installed as online assaulters can’t directly control the security of information like Kotlin interfaces. So, from today onwards, every Android developer whether he/she is a beginner or experienced must become well-versed with the use of Firebase Kotlin SDK for building high-quality apps running well on Android.

For CLI Installation of this extension, ensure you already add Firebase to your project if yes you follow below steps and command given bolow

Install an extension

$ firebase ext:install publisher-id/extension-id –project=projectId-or-alias

For more detail you can prefer official docs by Firebase(Click) 

3. Double.toPrice()

The Double.toPrice() Kotlin extension is a good choice for displaying prices in a more readable format. After you apply Double.toPrice() in java.text.DecimalFormat class, you will see how accurately this extension formats the prices represented in numbers thereby making the price much easier for the user to understand. If you are an Android developer or programmer, then you must try once this useful extension dealing well with prices an application displays in accordance with only one set of price formatting rules. 

4. Context.screenSize

Unable to calculate the screen size of your Android device or laptop at which Kotlin is installed? No need to worry as Context.screenSize returns the screen size with appropriate pixel values of length, breadth, and height. Programmatically, the below code snippet along with Context.screenSize extension will be helpful to understand more about Context.screenSize.

val Context.screenSize: Point

get() {

 val wm = getSystemService(Context.WINDOW_SERVICE) as WindowManager

 val display = wm.defaultDisplay

 val size = Point()

 display.getSize(size)

 return size

}

5. Nullable Receiver

This Nullable Receiver extension is precise and flexible with defining the extension functions with the class type (Nullable). Moreover, the extension ensures null safety in Kotlin by distinguishing well the nullable and non-nullable references which later eliminates the errors occurring at the time of compilation. If you aren’t using this extension, then there is a possibility of receiving this type of NPE (Null Pointer Exception)

Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type User?

6. String.isAlphanumeric

The String.isAlphanumeric extension validates whether the string is alphanumeric or not. You may now ask, “Kotlin. Check if the string is alphanumeric!!” Here, it is obvious to question what exactly is alphanumeric? See, an alphanumeric string only contains letters like a-z, A – Z, and numbers ranging from 0 to 9. Special symbols %, @, &, $ aren’t entertained by an alphanumeric string. To understand this extension better, run the below code on Online Kotlin Compiler.

fun isLettersOrDigits(chars: String): Boolean {

   return chars.matches(“^[a-zA-Z0-9]*$”.toRegex())

}

 fun main() {

   val chars = “Kotlin2020”

   println(“IsAlphaNumeric: ${isLettersOrDigits(chars)}”)

}

7. String.toLocation

This String.toLocation is basically a Kotlin String extension that converts a string to a location having some longitudinal (like 53.45) and latitudinal (-0.943) values. Also, there is no need to specify the location provider while using this extension. You must now use this extension like this:

import android.location.Location

fun String.toLocation(provider: String): Location? {

 val components = this.split(“,”)

 if (components.size != 2)

   return null

 val lat = components[0].toDoubleOrNull() ?: return null

 val lng = components[1].toDoubleOrNull() ?: return null

 val location = Location(provider);

 location.latitude = lat

 location.longitude = lng

 return location

}


Last Updated : 27 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads