Useful Kotlin Extensions for Android
Tags: technology, android, kotlin
Published: 2021-07-01 11:37:47

---
title: Useful Kotlin Extensions for Android
published: true
date: 2021-05-13 11:30:38 UTC
tags: Technology,android,kotlin
canonical_url: https://victorbrandalise.com/useful-kotlin-extensions-for-android/
---

Kotlin has many amazing features but one that stands out is extensions. Extensions are used to add functionality to existing classes.

They are extremely useful at helping to reduce boiler plate code. The good thing is that you can add them to any class, including the ones from third-party libraries that you can’t modify.

Most of the time we see _Extension functions_ but Kotlin also supports _Extension properties_.

## Useful Extension Functions

Lets start with some extensions that make the code easier to read, the ones that improve the code quality over time by reducing boiler plate code.

### Context

The Android API changes over time and something as simple as getting the color for a given ID can become not so simple. The extensions below helps reduce the boiler plate code associated with retrieving resources for a given ID.

<script src="https://gist.github.com/victorbrndls/3f0b031ed5a6cda4aae2f9376618699a.js"></script>
#### Usage
<script src="https://gist.github.com/victorbrndls/d15b0ba62204e94a47af88126366108c.js"></script>

We all know how repeating it’s to check whether the user has granted the permissions the app requires. The extension below can help with that. It doesn’t solve the whole problem but helps reduce the code size.

<script src="https://gist.github.com/victorbrndls/354d70bbcc6054a46b8a245b403f4bf9.js"></script>
#### Usage
<script src="https://gist.github.com/victorbrndls/0ddbfd804df33aa5d82f2901249c914b.js"></script>

Copying some text or URL to the clipboard should be something very simple. It only works with plain text but that’s what we use most of the time, if you need to support other formats you can change it.

<script src="https://gist.github.com/victorbrndls/81fb2f9d9b7256158ffdb668fc7096ea.js"></script>

Is there an URL in your app that the user needs to open the browser to view? Are you handling the case where the browser or equivalent app might be missing? The next extension abstracts most of what’s needed. You just need to give it the Uri and do something such as showing an error if there’s no app to resolve the intent.

<script src="https://gist.github.com/victorbrndls/157978afa5b67f13f6f5fc1e47cc38e7.js"></script>
#### Usage
<script src="https://gist.github.com/victorbrndls/61d07364867e3ac806010c28d3b39a7f.js"></script>
### Date

This extension is very useful if you need to log a date object over and over or send it via an API call. Something to notice here is that the ISO format is universal, it doesn’t change from country to country, I’ll show why that’s important later.

<script src="https://gist.github.com/victorbrndls/24b83e010e8db9818cff7d4b615547f9.js"></script>
#### Usage
<script src="https://gist.github.com/victorbrndls/c08c2ca8b8e59f6039ae0f79efb4d738.js"></script>

Some other extensions that might be useful

<script src="https://gist.github.com/victorbrndls/49a0b0830aa821d7cd5efe81370c716f.js"></script>
* * *

![](https://victorbrandalise.com/wp-content/uploads/2021/05/artur-tumasjan-42l3tjsJGyw-unsplash-1024x683.jpg)<figcaption>Photo by <a href="https://unsplash.com/@arturtumasjan?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Artur Tumasjan</a> on <a href="https://unsplash.com/s/photos/bad-lego?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption>

## All that glitters is not gold

Many things including extensions can be abused. Some of them look good, but are actually disguised code smells.

Let’s start with an useless extension. I was reading an article about extensions the other day and found this one.

<script src="https://gist.github.com/victorbrndls/c02d3a71304ab8cc69499066014779db.js"></script>
1. There’s no code reduction by using the extension. 
2. By using the extension the compiler will consider it a normal function call and refrain from optimizing the null case if possible. 
3. That’s bad use of OOP, we add methods to objects to “ask” them about something, null by definition is the absence of an something. An object is never null, what’s is null is the reference to an object.

### Be careful with localization

The extension below may look reasonable to most of us. It formats a Double to its decimal format.

<script src="https://gist.github.com/victorbrndls/b442437c0558f4b115ddfa1458049a0a.js"></script>

The dangerous thing about this code is that money is not formatted equally worldwide. In some countries you’ll see **1,200.00** (comma first, dot after), in others **1.200,00** (dot first, comma after). This is only a problem if the app is/will be used in many countries.

My solution would be to add the formatting pattern to `strings.xml` and create an extension on `Context` to handle that. That way you can define the monetary format for each language and still use the same function.

<script src="https://gist.github.com/victorbrndls/8c9fa4ae1bb089bf729b1704394a031f.js"></script>
* * *

## Extension Properties

Kotlin also allows us to add properties as extensions. I’ve not found an use case for them yet but it’s useful to know that it’s possible to use them.

<script src="https://gist.github.com/victorbrndls/650ae7c57e90f890836811892bca3f35.js"></script>
* * *

Thank you for reading. If there’s anything you disagree with, please leave a comment below so we can discuss it and arrive at a better solution.

* * *

## Other articles

[Android Lifecycle Scenarios – Single and Multi Activities](https://victorbrandalise.com/android-lifecycle-scenarios-single-and-multi-activities/ "Android Lifecycle Scenarios – Single and Multi Activities")

[Android Lifecycle Revised](https://victorbrandalise.com/android-lifecycle-revised/)

Cover photo by [Fran Jacquier](https://unsplash.com/@fran_?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/lego?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)

The post [Useful Kotlin Extensions for Android](https://victorbrandalise.com/useful-kotlin-extensions-for-android/) first appeared on [Victor Brandalise](https://victorbrandalise.com).