I\'m trying to use Kotlin with Butterknife for my Android Application.
Here is my build.gradle
dependencies {
...
compile \'com.jakewharton:b
you can import all synthetic properties for the free/res/layout/activity_main.xml layout by adding this import:
import kotlinx.android.synthetic.main.activity_main.*
Now you can access all views by using there id's no need to initiate findbyid
You can implement some extensions to improve your views behavior. Checkout this example for "onTextChange" in a regular editText:
fun EditText.onTextChange(callback: (text: CharSequence?, start: Int, before: Int, count: Int) -> Unit) {
addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
callback(s, start, before, count)
}
})
}
Usage:
m_editText.onTextChange { text, _, _, _ ->
m_textView.text = text
}
I vote for kotlin-android-extensions
Kotlin creators tell on their site that:
Kotlin Android Extensions plugin (automatically bundled into the Kotlin plugin in Android Studio) solves the same issue: replacing findViewById
with a concise and straightforward code. Consider using it unless you're already using ButterKnife and don't want to migrate.
and e.g.
// Using R.layout.activity_main from the main source set
import kotlinx.android.synthetic.main.activity_main.*
class MyActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView.setText("Hello, world!")
// Instead of findViewById(R.id.textView) as TextView
}
}
textView
is an extension property for Activity
, and it has the same type as declared in activity_main.xml
.
Add this in your Project Build.gradle
buildscript {
ext.kotlin_version = '1.1.2-4'
ext.butterknife_version = '8.6.0'
repositories {
maven { url 'https://maven.google.com' }
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-alpha1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.jakewharton:butterknife-gradle-plugin:$butterknife_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
And in your app Build.Gradle add this.
//Butterknife
compile "com.jakewharton:butterknife:$butterknife_version"
kapt "com.jakewharton:butterknife-compiler:$butterknife_version"
Jake Wharton created new library for kotlin called kotterknife: https://github.com/JakeWharton/kotterknife Gradle:
compile 'com.jakewharton:kotterknife:0.1.0-SNAPSHOT'
View:
val lastName: TextView by bindView(R.id.last_name)
// Optional binding.
val details: TextView? by bindOptionalView(R.id.details)
// List binding.
val nameViews: List<TextView> by bindViews(R.id.first_name, R.id.last_name)
// List binding with optional items being omitted.
val nameViews: List<TextView> by bindOptionalViews(R.id.first_name, R.id.middle_name, R.id.last_name)
In your app level build.gradle
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
kapt {
generateStubs = true
}
dependencies {
compile 'com.jakewharton:butterknife:10.2.0'
kapt 'com.jakewharton:butterknife-compiler:10.2.0'
}
In your Top Level build.gradle
buildscript {
ext.kotlin_version = '1.3.30'
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Activity
@BindView(R.id.toolbar) @JvmField var toolbar: Toolbar? = null
or
@BindView(R.id.toolbar) lateinit var toolbar: Toolbar
Inside OnCreate
ButterKnife.bind(this)