Kotlin KTX -set of extension for Android app dev



Kotlin KTX is nothing but a  set of extensions designed to make writing Kotlin code for Android more concise, idiomatic, and pleasant. Android KTX provides a nice API layer on top of both Android framework and Support Library to make writing your Kotlin code more natural.

Currently in preview, Android KTX provides an API layer on top of the Android framework and Support Library — the former is available now on GitHub. Google promises to make the other parts of Android KTX that cover the Android Support Library available in upcoming Support Library releases.

To start using Android KTX in your Android Kotlin projects, add the following to your app module’s build.gradle file:

Top 10 free Android libraries for app development in android studio

We will start with Animation class:

Animator functions

There’s a collection of extensions related to animations within the library, let’s take a quick look at what we have available in the current release!

Animation listener

To begin with, in KTX we can set an animation listener on an animator instance like this:

This allows us to receive the callbacks for an animation event. We can even add function for specific callbacks of the listener, you’ll only need to pass functions for the callbacks that you want to receive data for:

This itself is a massive reduction in the code for listener callbacks that we may not need and use.

Individual animation event listeners

We have the ability to listen for individual events in android, with KTX adding a pause listener can be done in the same way that the addListener() function:

Again, only requiring to pass in the functions that we require to be used for the callbacks.

We can also listen for individual animation events in a single line of code:

By comparing the same code we do in Java for animations in your Android projects, you’ll notice how much less code (and how much easier to read) this is. In each of these calls above, it represents the Animator instance that is in use.

Time operations

KTX also offers a collection of operations related to Time.

We can now access the DayOfWeek, Month and Year instances as an Int value with a simple call:

The Duration class also has a bunch of functions available:

The InstantLocalDataLocalDateTimeLocalTime properties can also be accessed with these functions:

Accessing properties of the MonthDayOffsetDateTime and OffsetTime classes can easily be done via these  calls:

These following functions are really helpful additions for the developer, these allow us to easily take an Int value and retrieve the corresponding representation for the given function call:

The same goes for a Long instance also, using these functions the following representations can be retrieved:


Most used are the below methods to update or call UI methods using handler and they are so short to call,thank God.

 Bundle class is short and nice:


Within the Util package there are a collection of functions related to files, arrays and other general data types.

To begin with, if you’re working with AtomicFiles you’ll be able to make use of the following functions:

For the LongSparseArraySparseArraySparseBooleanArraySparseIntArraySparseLongArray types we have all of the following functions available:(My Favorite)

Working with the Pair class now becomes a little easier:

We can also convert a Kotlin Pair directly to an Android Pair:


For SQLite there is a single extension function available at this time. Even so, it’s a pretty handy addition that allows us to perform a transaction using the given SQL statement.How Sweet


When it comes to resources in our android applications, there have been a collection of functions added for easing the process of working with the TypedArray class.


Most of the applications we work in are going to use text somewhere throughout the project and thankfully, KTX provides some extension functions when it comes to these parts. For text we essentially have some functions available for the SpannableStringBuilder class.

For example, after instantiating a Builder instance we can use the build methods to append some bold text:

There are also build functions to set the background color or wrap the text that you’re appending in spans:

For buildSpannedString extension function that allows us to build a string and use the provided builder actions to provide our styling:


when it comes to the Transition class we have a bunch of extension functions available for use. Just like the animation listeners, we can now easily listen for changes in our Transition by using the addListener() function call.

Also we have individual functions created for receiving callback  :


For the View class we have below functions. Setting callbacks for layout events is very clean:

Updating the padding for a view is now a lot cleaner and easier to do so, there are several functions available for it:

For converting a View instance to a Bitmap, you just need the single line of code!


We can now set the margins for our layout (params) instances with the following functions:


There are some handy ViewGroup related functions that you’ll likely be using in your projects! For example, checking a if a viewgroup contains a view:

Looping through the children of a viewgroup

Accessing the child at a desired position in Kotlin way:

We can perform other various viewgroup related operations:

With these functions also we have major changes also for Graphics class.

Master Gradle dependency management with Kotlin + buildSrc for Android

Do let me know how you feel about this post in comment section. I hope we will see more exciting changes in Kotlin – KTXwhich will make the life of Android developer easy and interesting.

I hope you enjoy this post related to Kotlin – KTX and started with the beautiful language Kotlin.