Kotlin, Zero to Kotlin Hero

Zero to Kotlin Hero : Kotlin Android Extensions

Yeah, Let’s talk about Kotlin Android Extensions ūüėé.

The previous article covers visibility modifiers in Kotlin. If you are not convinced about Kotlin yet, this article would probably convince you!

Kotlin has aided the improvement of the Android development experience. To add to that, an Android plugin by JetBrains called Kotlin Android Extensions was created to further promote easy Android development.

 

Kotlin Android Extensions. Who are they for?

If you’re already an Android developer or you are starting out as one, Kotlin Android Extensions are a great library for your Android development toolkit.

One example would be findViewById(). If you are an Android developer, you probably use findViewById() a lot and my guess is that you’re as tired as I am. To replace findViewById() in Java, Jake Wharton built this amazing ButterKnife¬†library for recovering views. However, to use ButterKnife (and other similar libraries), we need to annotate the fields for each exposed¬†View.

If you’re a fan of ButterKnife, chances are that you would like Kotlin Android Extensions. Using Kotlin Android Extensions, you can reference xml¬†views from Kotlin code in an approach similar to the snippet below:

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

class MyActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        // Instead of findViewById<TextView>(R.id.textView)
        textView.setText("AdoraHack!")
    }
}

 

How to use Kotlin Android Extensions?

To use Kotlin Android Extensions, the first step is to add the extension in the modules build.gradle file like this:

apply plugin: 'kotlin-android-extensions'

Once this is done, we are ready to use Kotlin Android Extensions!

If we have a file MainActivity.kt whose corresponding xml file is activity_main.xml, we can import all the properties in the xml file by adding this import statement to our code:

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

Once we have this, we can refer to any layout in the xml file by its Id and if that layout does not exist we get an error and the code will not run until we fix it. This is very different from findViewById that throws an exception at runtime.

 

How do you refer to a layout?

If we have an xml file like the one below:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <TextView
        android:id="@+id/hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Adora!"/>


</LinearLayout>

All we have to do in our Kotlin code after importing the extension is:

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


class MainActivity : Activity() {
  
   override fun onCreate(savedInstanceState: Bundle?) {

      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      hello.text = "Hello Nenne" //No need for findViewById(R.id.hello) we can simply just use hello

   }

}

 

 

Conclusion

We have seen Kotlin Android Extensions and how its easy to refer to a layout. At this point, I’m hoping¬†you are convinced and are Team Kotlin now. Get ready to say goodbye to nulls in the next article!

 

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *