You have 2 free member-only stories left this month.
5 More Kotlin Extensions for Android Developers
Online status, permissions, spannable strings, and more
You might have already gone through a bunch of articles related to Kotlin extensions, but this article is not all about extensions. It’s about making your code expressive, so I’ve compiled a list of my top extensions that keep code as natural as possible.
This is the second part of this series. If you’re new to Kotlin or the concept of extensions, I would highly recommend reading the first part to learn the basics.
1. isOnline
A network request is an inevitable task to perform in most mobile applications. Checking whether the user is connected to the internet or not is crucial. Taking this to the next level, a few apps show a small snackbar-type banner at the top or bottom of the UI to indicate that the user is online/offline.
Checking if the user is connected to the internet or not is almost a daily task for any mobile developer. As such, the code we implement should be clutter-free and express itself without any prior knowledge.
I use Kotlin extensions primarily for these kinds of use cases. Mostly, we try to check the internet connectivity in our fragments, activities, services, etc. One of the common things among them is Android Context
. We can create an extension function on the Context
class called isOnline
and implement the code, as shown below:
This function returns true
if the user is online. Otherwise, it returns false
. We can also use lambdas and higher-order functions in Kotlin to make it more declarative. Have a look:
Here, we’ve removed the boolean return type and used higher-order functions to pass the functions that should be executed in their respective events. So now we don’t need to use the if
statement at the call site. Instead, we use the lambda function:
2. Show or Hide the Keyboard
Android keyboard events are a mess to maintain. Recently, the Android team has been focusing on this issue with solutions like window insets and more. To be frank, most developers haven’t adopted them yet. In my daily routine, I use the following extension functions to show and close the keyboard.
Before Kotlin, I used these functions as Utility
class static functions and invoked them from a singleton instance, which is a geekier approach. Then I decided to make it as natural as possible to trigger, so I created two view extension functions to open and close the keyboard:
3. Permission Check
This is one of my favorite extensions because it increases my productivity and enhances code readability at the same time. Google introduced dynamic permissions for Android 6. It’s a good approach for security, but the implementation part involves an awful lot of boilerplate code.
There are few great libraries to make it easy in your day-to-day development, such as Dexter, PermissionsDispatcher, and EazyPermissions. But these solutions often involve adding more unnecessary code in your code base.
As a simple alternative to all these approaches, we can create a few extension functions to make it easy:
Note: I’ve adapted these extensions from William Gouvea’s article.
4. Spannable Strings
No matter which platform you’re working on, there will be times when you want to highlight a certain part of the string to get user focus or you want to assign a click listener to a particular part of the string. This can be achieved in the Android environment using spannable strings.
Here are a few extensions that I use to simplify spannable string code:
Using these extensions, we can write clutter-free code at the call site.
5. Color Conversion Extensions
In Android, we usually declare colors in HEX format. But it’s not only the format that we will encounter. Some of the popular libraries use RGB and integers, so it’s important that we can quickly convert to any color format. For this purpose, I use the following extensions:
These are the two extensions that make my life easy when I’m dealing with color palettes. Of course, you can create your own extensions based on your needs. For instance, you can create RGB-to-HEX if that’s what you want.
Bonus
To learn more about coroutines and other advanced features of Kotlin, read the following articles:
- “Advanced Programming With Kotlin”
- “Kotlin Coroutines, From the Basic to the Advanced”
- “Asynchronous Data Loading With New Kotlin Flow”
- “Why and How to Use Kotlin’s Native Serialization Library”
That is all for now. I hope you learned something useful. Thanks for reading.