Alternative solution to view binding other than ButterKnife
See original GitHub issueDisclaimer: this is a copy-paste from #395 as it seems that I was late to the discussion and I feel that my points warranted a separate issue.
One problem with using Butterknife (though minor) is that the fields must be at least package-private, which means the scope is unnecessarily broad in some circumstances.
An alternative that I am currently using in one of my projects is to use the generated view binder found using the data binding. Using this doesn’t mean that you have to use any of the features of the data binding library (nor would I advocate for such a thing). The main benefit that I found using the generated view binder is that it is type safe, something that neither Butterknife nor the standard Android findViewById
offers i.e. you don’t have to cast the findViewById
to a subclass of View
.
Jake Wharton (the writer of Butterknife) even says that the use of it can be advantageous (source). Here’s an article which tells you basically all that you need to know about this approach.
I thought I’d just add this to the discussion as it doesn’t get talked about much, but would reduce the boilerplate code even more than that of Butterknife alone, is type safe, and IIRC findViewById
traverses the layout at runtime whereas using the view binding aspect of databinding, view processing is done at compilation time.
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (3 by maintainers)
Top GitHub Comments
@whym Given that the generated view binder can be used in conjunction with Butterknife, should we merge #409 and just consider the addition later?
It seems that the project has settled on Butterknife and the generated view binder hasn’t seen any use.