I migrated a project from butterknife to android viewBinding.
In Butterknife there is something like:
@OnClick(R.id.button)
fun onButtonClicked()
And now I have to set all the button click listener like this:
binding.button.setOnClickListener { onButtonClicked() }
...
Is there a way to implement a custom annotation to be able to write something like:
@OnClick(binding.button)
fun onButtonClicked()
And now I have to set all the button click listener like this:
Yes, This is the right way of doing click listeners with View Binding.
Is there a way to implement a custom annotation to be able to write something like:
No. There isn't any out of the box solutions available for it. but you can create your own annotation processor for handling click listeners. Follow this article for more info.
If you ask for my personal opinion I will follow the default way and use setOnClickListener
. Jake Wharton, the creator of Butterknife
suggests using View Binding (He is also part of the team at Google which is working on View Binding)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With