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,


	android:layout_gravity="center" />
	android:layout_gravity="center" />


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?) {


	// for TextView
	welcome.text = "Welcome user ! "

	// for ImageView

The same code in java will we written as,

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity{
	protected void onCreate(Bundle savedInstanceState){
		//for TextView
		TextView welcome=(TextView)findViewById(R.id.welcome);
		welcome.setText("Welcome User,");

		// for ImageView
		ImageView image = (ImageView) findViewById(R.id.imageView);


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.


Comments and Discussions!

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.