Useful extensions

Sometimes we can’t use kotlin synthetics library.

Maybe layout was changed during runtime, and int this case some new views can’t be accesseed with synthetics library, because view binding process starting on view creation stage.

Of course, we can use old findViewById way, but I try to use most powerful and elegant approach with kotlin extensions and lazy initializing:

fun <T : View> Activity.bindView(@IdRes idRes: Int): Lazy<T> = lazy(LazyThreadSafetyMode.NONE) { findViewById<T>(idRes) }

Now we can find view using this delegate:

val screenText: TextView by bindView(


val screenText by bindView<TextView>(

That’s perfect!

Leave a reply

Your email address will not be published.

You may also like

More in:Android