Home »
Android with Kotlin
Kotlin Android Extension | Android with Kotlin
In this article I am covering an important plugin and most interesting feature of kotlin i.e. kotlin-android-extension.
Submitted by Aman Gautam, on August 23, 2018
I found that I should discuss it before moving further as we will be using it recurrently. Since we have started building our apps using kotlin, this plugin will surely make you fall in love with kotlin. If you haven’t read previous articles yet, it is recommended that you should have an eye over them to get the basic overview.
Kotlin-android-extension is a plugin that comes with many features such view binding, Parcelable implementation generator etc. In this article, we will discuss mainly view binding.
If you have ever developed an android app using Java, you may probably get tired of using findViewById() for view binding. Mostly developer prefers Butterknife library for the same. But kotlin has made all that simple. Just enable the plugin and you are all set.
Configuring the extension
Since it is a part of the Kotlin plugin for IntelliJ IDEA and Android Studio. You do not need to install any additional plugin.
All you need is to enable the Android Extensions Gradle plugin in your module's "build.gradle" file:
apply plugin: 'kotlin-android-extensions'
Import synthetic properties
To include all widgets in one go, you need to write one line in the import section in kotlin file. It will automatically bind views to their respective names.
import kotlinx.android.synthetic.main.<layoutName>.*
Suppose if layout name is "activity_main.xml", then you have to write:
import kotlinx.android.synthetic.main.activity_main.*
Suppose the "activity_main.xml" is as follows,
<LinearLayput
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center" />
</LinearLayput>
Once we have imported all view we can use them as follows,
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// for TextView
welcome.text = "Welcome user ! "
// for ImageView
imageView.setImageResource(R.drawable.ic_launcher)
}
}
The same code in java will we written as,
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//for TextView
TextView welcome=(TextView)findViewById(R.id.welcome);
welcome.setText("Welcome User,");
// for ImageView
ImageView image = (ImageView) findViewById(R.id.imageView);
image.setImageResource(R.drawable.android_image3);
}
}
Conclusion:
So we can see how efficiently we can bind views using kotlin-android-extension. This is one of its features. If you face any doubt, feel free to write in the comment section below. In the next articles, we will try to get deeper insights into android with kotlin. Till then Good Bye and have a nice day.