Skip to content

Android KTX: Sweeter Kotlin Codes!

Google has released the preview version of Android KTX which is an extension of Kotlin library for Android Development. Even though it makes more idiomatic and easier approaching on coding in Kotlin, It is still available in separate package name begins with `androidx`. They also mention that the package will be committed to API compatibility after user feedback. Android KTX can be bundled with your app using the below snippet to your gradle file.

gradle.
repositories {
    google()
}

dependencies {
    implementation 'androidx.core:core-ktx:0.1'
}

Here are some cool codes that you might meet on Android KTX (Other than the pre-mentioned in the youtube video/Developers blog).

1. Adding view padding and updating them made easy by passing the value as an argument. You can set padding equally or else update padding side by side also using below methods.

//set padding to view
fun View.setPadding(size: Int): Unit

//update padding of the view
fun View.updatePadding(left: Int = paddingLeft, top: Int = paddingTop, right: Int = paddingRight, bottom: Int = paddingBottom): Unit

//update padding relatively
fun View.updatePaddingRelative(start: Int = paddingStart, top: Int = paddingTop, end: Int = paddingEnd, bottom: Int = paddingBottom): Unit

2.Making bitmap from view made easy. You can simply pass the view as the function argument. There is a default param value for Bitmap configuration which is set to ARGB_8888.

//converts view to bitmap object
fun View.toBitmap(config: Config = Bitmap.Config.ARGB_8888  ): Bitmap

3.ViewGroup members manipulation is added via below function. Array-like treatment made them easier for implementing logic.

//Iterates over views in group
fun ViewGroup.forEach(action: (view: View) -> Unit): Unit

//Iterates over views with sequential index
fun ViewGroup.forEachIndexed(action: (index: Int, view: View) -> Unit): Unit

//Returns view for a specific index
operator fun ViewGroup.get(index: Int): View

//Adding and removing of view from view group

operator fun ViewGroup.minusAssign(view: View): Unit
operator fun ViewGroup.plusAssign(view: View): Unit

//Check whether view group is empty or not
fun ViewGroup.isEmpty(): Boolean
fun ViewGroup.isNotEmpty(): Boolean

//Get size of the view group
val ViewGroup.size: Int

4. Converting bitmap object to Adaptive and normal icons are possible via KTX methods. This will help to update the shortcut icons and remote widget icons easier and flexible. Also, you can convert a Uri object to an Icon object directly via KTX method.

//Convert  bitmap to adaptive icon
fun Bitmap.toAdaptiveIcon(): Icon

//Convert bitmap to bitmapdrwable
fun Bitmap.toDrawable(resources: Resources): BitmapDrawable

//Convert bitmap to icon
fun Bitmap.toIcon(): Icon

//Convert drawable to bitmap
fun Drawable.toBitmap(width: Int = intrinsicWidth, height: Int = intrinsicHeight, config: Config? = null): Bitmap

//Create icon from Uri object
fun Uri.toIcon(): Icon

5. And more…

    //Create color drawable from color code
    fun Color.toDrawable(): ColorDrawable

    //Update drawable bounds 
    fun Drawable.updateBounds(left: Int = bounds.left, top: Int = bounds.top, right: Int = bounds.right, bottom: Int = bounds.bottom): Unit

    //Create color drawable from color value
    fun Int.toDrawable(): ColorDrawable

    //Return the handle to system -level service
    fun <t> Context.systemService(): T

    //Return content value class which includes key/value pairs
    fun contentValuesOf(vararg pairs: Pair(String, Any?): ContentValues

Still, there are lots of Kotlin extensions added in the preview. You can also request for more common utility functions through this
GitHub repo.

You may also feel that Android KTX is nothing but they are just some code for reducing code length. But think about having a cherry on top of the cake like KTX makes the effort of coding less and making it more and more sweeter! Android KTX hopes to get a more updates on upcoming releases.

Happy coding!

Published inAndroidKotlin

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *