Good bye findViewById() use Kotlin Android Extensions

Good bye findViewById() use Kotlin Android Extensions

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 ….

Step 1.

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.

Step2.

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.

import kotlinx.android.synthetic.main.activity_main.*

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 findViewById.

Simple Call Xml View by name .

textView.setText("Msg")

Here textView is my text view component inside xml.

 

Share this post

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.


%d bloggers like this: