ViewBindingHelper

Additional

Language
Kotlin
Version
1.0.2 (Oct 19, 2020)
Created
Oct 15, 2020
Updated
Oct 21, 2020 (Retired)
Owner
Daniil Karamyshev (danl12)
Contributor
Daniil Karamyshev (danl12)
1
Activity
Badge
Generate
Download
Source code

Advertisement

ViewBindingHelper

Makes it easier to work with Android View Binding.

Install

android {
    ...
    buildFeatures {
        viewBinding true
    }
}

dependencies {
  ...
  implementation 'com.danl.viewbindinghelper:viewbindinghelper:1.0.2'
}

Proguard rules

-keep class com.example.app.databinding.* {
    <methods>;
}

Usage

Activity

class MainActivity : ViewBindingActivity<ActivityMainBinding>() {

    override fun onBindingCreated(savedInstanceState: Bundle?) {
        binding.textView.text = "Hello World!"
    }
}

Fragment

class SampleFragment : ViewBindingFragment<FragmentSampleBinding>() {

    override fun onBindingCreated(savedInstanceState: Bundle?) {
        binding.textView.text = "Hello World!"
    }
}

DialogFragment

class SampleDialogFragment : ViewBindingDialogFragment<DialogSampleBinding>() {

    override fun onBindingCreated(savedInstanceState: Bundle?) {
        binding.textView.text = "Hello World!"
    }
}

Dialog

val dialog = Dialog(requireContext())
dialog.bind<DialogSampleBinding> {
    textView.text = getString(R.string.bind_dialog)
}
dialog.show()
dialog.binding<DialogSampleBinding>().let {
    Toast.makeText(
        requireContext(),
        it.textView.text,
        Toast.LENGTH_LONG
    ).show()
}

AlertDialog

AlertDialog.Builder(requireContext()).bind<DialogSampleBinding> {
    textView.text = getString(R.string.bind_dialog)
}.show()

View

val view = View.inflate(requireContext(), R.layout.dialog_sample, null)
view.binding<DialogSampleBinding>().run {
    textView.text = getString(R.string.bind_view)
}