A viewbinding delegate. This is based on Simple one-liner ViewBinding in Fragments and Activities with Kotlin by Zhuinden.
project build.gradle
allprojects {
repositories {
// ...
maven { url 'https://jitpack.io' }
}
}
app build.gradle
android {
// ...
buildFeatures {
viewBinding true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}
dependencies {
// ...
implementation 'com.github.coderbuck:viewbinding-ktx:1.0.0'
}
in AppCompatActivity or Fragment
val binding by viewBinding(ActivityMainBinding::bind)
note: replace ActivityMainBinding with your own
In fragment you can only use the binding
field after onCreateView
and before onDestroyView
(not included).
Copyright 2020 CoderBuck
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.