Awesome Kotlin Extensions for your project

Kotlin extensions are one of my favourite kotlin features! It gives you freedom of creating new functions for some old or big classes, which you don’t really want to override. Recently I found some great article with bunch of very useful kotlin extensions which can be used in our Android projects. Let’s take a look at two examples:

getCompatColor()


fun Context.getCompatColor(@ColorRes colorId: Int) = ResourcesCompat.getColor(resources, colorId, null)

// Activity
getCompatColor(R.color.white)

// Fragment
requireContext().getCompatColor(R.color.white)

hasPermissions()

fun Context.hasPermissions(vararg permissions: String) = permissions.all { permission ->
    ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED
}

// Usage (Activity)
hasPermissions(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION)

Those and some other you can find in this article: https://victorbrandalise.com/useful-kotlin-extensions-for-android/?utm_source=newsletter&utm_medium=email&utm_campaign=funkcje_rozszerzajace_ktore_warto_znac&utm_term=2021-10-01

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *