Kotlin android Extension: Goodbye to findViewById (KAD 04), kotlinfindviewbyid
By Antonio Leiva
Time: Dec 12,201 6
Link: http://antonioleiva.com/kotlin-android-extensions/
You may be tired of using findViewById to write Android views day after day. Or you may discard it and use the famous Butterknife library. Then you will love Kotlin's Android extension.
Kotlin Android Extension
The Android extension of Kotlin is one of the regular extensions of the Kotlin plug-in. It seamlessly covers the Activities view and Fragments y view.
Let's see how simple it is.
Integrate the Android extension of Kotlin in our code
Although you can use a plug-in to integrate it into the code, you still need to add additional apply in the Android module:
1 apply plugin: 'com.android.application'2 apply plugin: 'kotlin-android'3 apply plugin: 'kotlin-android-extensions'
These are all you need to add. In this way, you are ready to use it.
Overwrite the view in Activity or Fragment
In this case, overwriting the view in your Activity or Fragment is as convenient as defining the view id directly in XML.
Imagine you have such XML:
1 <?xml version="1.0" encoding="utf-8"?> 2 <FrameLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent"> 6 7 <TextView 8 android:id="@+id/welcomeMessage" 9 android:layout_width="wrap_content"10 android:layout_height="wrap_content"11 android:layout_gravity="center"12 android:text="Hello World!"/>13 14 </FrameLayout>
As you can see, TestView has a welcomeMessage id.
You only need to write in your MainActivity as follows:
1 override fun onCreate(savedInstanceState: Bundle?) {2 super.onCreate(savedInstanceState)3 setContentView(R.layout.activity_main)4 5 welcomeMessage.text = "Hello Kotlin!"6 }
To be able to use it, you need to specifically import (this sentence is written below), and the IDE can automatically add the import (import) it. Isn't that easy!
import kotlinx.android.synthetic.main.activity_main.*
The code generated by the plug-in can store the View cache, so that when you access the view again, you do not need another findViewById.
Overwrite other views with one view
We have the following view:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:orientation="vertical" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 6 <ImageView 7 android:id="@+id/itemImage" 8 android:layout_width="match_parent" 9 android:layout_height="200dp"/>10 11 <TextView12 android:id="@+id/itemTitle"13 android:layout_width="match_parent"14 android:layout_height="wrap_content"/>15 16 </LinearLayout>
If you add an adapter to it.
You only need to use this plug-in to directly access the sub-View:
1 val itemView = ...2 itemView.itemImage.setImageResource(R.mipmap.ic_launcher)3 itemView.itemTitle.text = "My Text"
Although the plug-in also helps you fill in the import, this type is slightly different:
import kotlinx.android.synthetic.main.view_item.view.*
There are some things you need to know:
However, you only need to use it carefully, and it is still a very useful tool.
Conclusion
You already know how to easily process the Android view in Kotlin. With a simple plug-in, we can ignore all the bad code related to view recovery after expansion. This plug-in will generate the correct type without any problems according to our required features.