ButterKnife  – A Viewbinding library for Android

ButterKnife  – A Viewbinding library for Android

In this post, we will detailed look into ButterKnife, a viewbinding library for android. Also demonstrate how it’s work, how to integrate and use in your android app development to make your development faster.

Let’s get started!

What is Butterknife?

Butter Knife is small, simple and lightweight, and it makes life as a developer easier. It is developed by Jake Wharton. It makes your code less and more clear. It is time saving to write repetitive lines of code.

ButterKnife is a view binding library that uses annotation to generate boilerplate code for us.

It allows developers to perform injection on arbitrary objectsviews and OnClickListeners so they can focus on writing useful code. Consider Android Butter Knife a reduction library. It replaces findViewById with @BindView() and set^^^^Listener calls with @onClick() making code cleaner and more understandable. Butter Knife enables focus on logic instead of glue code and reduces development time by reducing redundant coding.

Getting Started

It is easy to use butterknife in your project. Just add the following dependencies in your build.gradle(module: app) and sync your project!

Butterknife supports Field and method binding for Android views which uses annotation processing to generate boilerplate code for you.

  • Eliminate findViewById calls by using @BindView on fields.
  • Group multiple views in a list or array. Operate on all of them at once with actions, setters, or properties.
  • Eliminate anonymous inner-classes for listeners by annotating methods with @OnClick and others.
  • Eliminate resource lookups by using resource annotations on fields.

All you need to write in these methods is the following:

Activities:

Fragments:

In both the cases Butter Knife binds to the specified Android Activity or Viewtargets to correctly act on the injections specified in these classes. All the view finding lines of code and the action listeners are implemented elsewhere, leaving these methods as clean as possible.

Binding Views, Resources and Event Listeners

Butter Knife replaces the standard findViewById call with @BindView, followed by a view ID and it automatically casts the corresponding view in the corresponding layout. This representation of the code makes it more compact and readable.

Here’s an example of this “injection”:

You can do the same with app resources like StringDrawable and dimensions (dimenvalue , etc).

Butter Knife also makes setting event listeners of Android layout Views easy and removes the listeners implementation from the key onCreate and onCreateViewmethods.

You can refer to the Butterknife Documentation on this link

Conclusion

Android Butter Knife helps you create cleaner and tidier code, handling a lot of the ugly boilerplate code that Android has become infamous for. Have you tried it in your projects? Thanks for reading this article. Be sure to share this article if you found it helpful.

Happy Coding!

Leave a Reply

Your email address will not be published. Required fields are marked *