Home »
Android with Kotlin
Kotlin Android Extension | Android with Kotlin
By IncludeHelp Last updated : December 04, 2024
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
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 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.*
XML Layout File
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>
Using Views in Kotlin
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)
}
}
Equivalent Java Code
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.