Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Annotation like Butterknifes "@OnClick" for android viewBinding

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()
like image 979
Punika Avatar asked Nov 06 '22 09:11

Punika


1 Answers

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)

like image 131
Somesh Kumar Avatar answered Nov 14 '22 22:11

Somesh Kumar