10 Useful Kotlin Extension Functions for Android Developers #2

Expand your productivity with remarkable Kotlin extensions

Akshay Kalola
4 min readApr 30, 2023

Thanks for showing your love for the Kotlin extension functions in the first part. I would like to share more extensions to help you improve your programming experience with Android and Kotlin. If you have not read it yet, you can check it out at the link below.

Prerequisite

Should have basic knowledge of kotlin syntax and android

Start working with new chapter…

Value from EditText

Through the text property of EditText, you can quickly obtain the text from EditText. But, it’s Editable. So, we have to convert it to a string for each and every time to get the exact value from EditText. But, here is the catch, you can easily get value from EditText using the extension property listed below.

Usage

val name = etName.value

Start Activity

Start Activity is a common way to transition to another activity. You must first address the intent of the target activity. However, by using this extension function, you can eliminate the intent creation part.

You can also able to customize the intent to pass some data and other things. It’s also able to kill the current calling activity if you want. Look at the examples below.

Usage

startActivity(MainActivity::class.java) // Without Intent modification
startActivity(MainActivity::class.java) {
// You can access the intent object in this block
putExtra("key", "value")
}

Check Network

Nowadays, all our applications have internet connectivity as per our requirements, features, and functionality. So, you may have to check whether an internet connection is available or not.

So, this extension function is useful for checking the internet connection in activity and fragment. It is simple to utilize in the if statement and other locations inside the scope.

Usage

if (isNetworkAvailable()) {
// Called when network is available
} else {
// Called when network not available
}

Check Permission

To fulfill our use cases, you might occasionally have to play with any permission. So, we may have to verify whether the permission is granted or not for our application. Therefore, the extension function below is useful for determining whether or not permission has been given.

Usage

if (isPermissionGranted(Manifest.permission.ACCESS_FINE_LOCATION)) {
// Block runs if permission is granted
} else {
// Ask for permission
}

Remove whitespaces

Sometimes we may have unrealistic text data from Rest API or some other data source. So, if you have more than one whitespace and you want to remove it then we can use removeDuplicateWhitespaces() or if you want to completely remove it then you can use removeAllWhitespaces()extension functions.

Usage

"Hello,     world!!!".removeAllWhitespaces() // Output: Hello,world!!!
"Hello, world!!!".removeDuplicateWhitespaces() // Output: Hello, world!!!

toEditable()

Editable is an interface for text whose content and markup can be changed. If you want to convert string to editable then you have to deal with an editable factory instance.

Now, using the below function you can easily remove boilerplate code. You can use toEditable() in any string function. And, You can use it to assign text to EditText Field.

Usage

etName.text = "First name".toEditable()

Screen Size

Working with the device screen size may be necessary if you are dealing with a dynamic view layout based on some state or data. Therefore, this extension property will be utilized for that. It’ll give the height and width of the device. You can use it on the activity scope or use context objects at other places.

Usage

val size = screenSize
val deviceHeight = size.height
val deviceWidth = size.width

System Service Managers

These properties are used to access the system manager directly without creating an object of it. You can use it directly in the activity and for the fragment you have to use the context. Please take note that the properties listed below are not all of them. But if more is needed, you can add more.

Usage

val manager = downloadManager // In Activity
val manager = requireContext().downloadManager// In Fragment

Copy to clipboard

You may occasionally need to copy text to the clipboard to allow the user to share text with other apps. As a result, you can utilize the extension property listed below on a string object and you will be good to go.

Usage

"This is clipboard".copyToClipboard(context)

Boolean Expressions

Boolean, it’s an essential need for every programmer. You have to work with the boolean every single day in your programming life. In kotlin, you might have to deal with nullability and boolean together in that case you can utilize these extensions. Here, we have also used the concept of the contract to achieve smart casting.

Usage

lateinit var any: Boolean? // Assume that, this property is already assigned
if (any.isTrue()) {
// Run when any is true only
}
if (any.isFalse()) {
// Run when any is false only
}
val any1: Boolean = any.orTrue // If any is null then any1 = true otherwise any1 = any
val any2: Boolean = any.orFalse // If any is null then any1 = false otherwise any1 = any

Conclusion

In this blog post, we have explored Kotlin extension functions for Android developers. These extensions can simplify common tasks such as working with strings, handling dates and times, managing networks, and more. They can save time and effort, reduce errors, and enhance the overall quality of your code.

Thank you for taking the time to read it. 👏 You can give me a clap for this article up to 50 claps.

You can connect with me 😊 on Twitter, LinkedIn or GitHub.

Did I forget something? 🤔 Please comment below.

--

--

Akshay Kalola

Experienced mobile app developer with over four years of hands-on experience. Working on Native Android Development and Flutter Cross-Platform Framework.