Good bye findViewById() use Kotlin Android ExtensionsVivek Kumar kanaujia
Hi Developers as we always juggle with findViewById() in Android activity class . Some time we forget to typecast xml into Kotlin or java . So good news for Kotlin android developer.
Kotlin Android Extensions
Kotlin Android Extensions are another Kotlin plugin that is included in the regular one, and that will allow to recover/find views from Fragments, Activities, and Views in an effective way.
see how easy it is ….
Add Kotlin Android Extensions in Gradle (Project)
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' //this the main plugin
And that’s all you need to sync your gradle. You’re now ready to start working with it.
Recovering/find views in an Activity or Fragment
import your xml layout like below code in activity . here my activity xml file name is activity_main it would be different in your case. so add it in your activitity.
The code that the plugin generates is able to store a view cache, so if you ask the view again, this won’t require another
Simple Call Xml View by name .
Here textView is my text view component inside xml.