I'm trying to use Kotlin with Butterknife for my Android Application.
Here is my build.gradle
dependencies {
...
compile 'com.jakewharton:butterknife:8.0.1'
kapt 'com.jakewharton:butterknife-compiler:8.0.1'
}
kapt {
generateStubs = true
}
I also has an EditText and I want to show a message using ButterKnife when it is changed:
@OnTextChanged(R.id.input)
fun test() {
toast(1)
}
However, nothing happens. I put a breakpoint into the function - and it is not even executed.
P.S: I have heard about kotterknife, however I have seen an example with pure Butterknife.
What am I doing wrong?
Add this in your Project Build.gradle
And in your app Build.Gradle add this.
In your gradle:
In your activity
Of course, remember
ButterKnife.bind(this)
and apply the plugin on the top of your app.gradleapply plugin: 'kotlin-kapt'
Check full example
Full Link: https://github.com/JetBrains/kotlin-examples/tree/master/gradle/android-butterknife
You can implement some extensions to improve your views behavior. Checkout this example for "onTextChange" in a regular editText:
Usage:
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.
textView
is an extension property forActivity
, and it has the same type as declared inactivity_main.xml
.Jake Wharton created new library for kotlin called kotterknife: https://github.com/JakeWharton/kotterknife Gradle:
View:
In Kotlin, actually there is no need (or) necessity for going ButterKnife concepts. because in your activity you can directly refer the view _id of the layout file as referred below.
layout.xml
Activity.kt
build.gradle(app)