Ditching the findViewById() method

In the world of Android, findViewById() is the necessity. And we know how painful it is to code it every time for every view. Yes, a butter knife will give some relief, but it has its own disadvantages (we will not go into them). Since Kotlin is targeting for Android app development, it has to provide the solution to this problem. And Kotlin's kotlin-android-extensions is the solution.

If the following is your view:

    <com.twitter.sdk.android.core.identity.TwitterLoginButton       android:id="@+id/loginWithTwitter"       android:layout_width="wrap_content"       android:layout_height="wrap_content" />

A typical Java code would look as follows:

    TwitterLoginButton loginWithTwitter = (TwitterLoginButton)   findViewById(R.id.loginWithTwitter); ...

Get Kotlin Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.