You have 2 free member-only stories left this month.

5 More Kotlin Extensions for Android Developers

Online status, permissions, spannable strings, and more

Crayons
Crayons
Photo by Ave Calvar on Unsplash.

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:

That is all for now. I hope you learned something useful. Thanks for reading.

Better Programming

Advice for programmers.

Sign up for The Best of Better Programming

By Better Programming

A weekly newsletter sent every Friday with the best articles we published that week. Code tutorials, advice, career opportunities, and more! Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Thanks to Anupam Chugh. 

Siva Ganesh Kantamani

Written by

Learn. Code. Write. Repeat. Visit me at https://about.me/sivaganesh_kantamani & Join my email list at https://sgkantamani.substack.com/p/subscribe

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium