ViewBindingHelper

Additional

Language
Kotlin
Version
1.0.2 (Oct 19, 2020)
Created
Oct 16, 2020
Updated
Oct 19, 2020
Owner
Dan (danl12)
Contributor
Dan (danl12)
1
Activity
Badge
Generate
Download
Source code

Advertising

ViewBindingHelper

Makes it easier to work with Android View Binding.

Install

android {
    ...
    buildFeatures {
        viewBinding true
    }
}

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

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)
}