Last Updated:

ButterKnife library in Android - With Working Example

ButterKnife library in Android

In this tutorial, we'll take a look at the benefits of using the popular ButterKnife Android library. We will find out why it is needed and when it is better to use it.

A brief overview of the ButterKnife library in Android

 

ButterKnife is a binding tool that uses annotations to generate boilerplate code. The main task of the library is to save us from redundant code, multiple use when working with View. Instead, concise annotations are used and the code looks much cleaner and more elegant.findViewById(R.id.some_view)

To start using the ButterKnife library, add the following lines to the dependencies section:

Note:

  • The last 3 digits (8.4.0 - version of the library) will certainly change, so when you use this code in your application, do not forget to look at the current version in the repository on Github.
  • Don't forget to paste the dependencies to work correctly with the annotations.annotationProcessor

Also, Android Studio may require you to have a more up-to-date version of the Android Support Repository and will offer to install the update itself – agree.

As a result, your script should look something like this:build.gradle

 

ButterKnife annotation overview

Below are the most popular and useful annotations of the ButterKnife library. They affect working with widgets within an Activity, in snippets, working with resources, event handling, and more.

We have a activity_main.xml that describes some widgets.

 

Task: we need to initialize them in code and somehow use them in the future. Below are the solutions that ButterKnife represents.

Abstract @BindView

 

The most popular is the annotation, which is responsible for automatically associating the described View with its xml representation in the layout file by the specified identifier.@BindView

Learn more about annotations in Java.

1.1 Example @BindView

 

The generated code for the above example is roughly equivalent to the following:ButterKnife.bind(this)

 

1.2 Linking resources in ButterKnife

The library already has predefined annotations for working with resources: , , , , , which associates the identifier with the declared widget. An example of initializing some resources using ButterKnife:@BindInt@BindString@BindDimen@BindDrawable@BindBool@BindColor

 

 

2. Out-of-activity usage example@BindView

If we want to use ButterKnife in some fragment or adapter, then we need to explicitly specify the View that we want to use to bind the widgets.

This approach also works in the adapter, for example, when we work with a ListView. Let's see an example:

 

3. Annotation @BindViews — Working with multiple Views

 

ButterKnife provides an annotation to easily work with a variety of View objects. For example, we define an array or list of widget identifiers and apply() with some action. The actions are predefined by the programmer using the Action and Setter interfaces. An example below:@BindViewsButterKnife.

Let's set the list:

 

Now apply some action to the list:

The specified action will now be applied to the View set with just one call to the .apply()

Abstract @OnClick

With ButterKnife in Android, you can ask listeners with just one annotation:

Note that the method argument is not required, meaning we could just write .performSend()public void performSend() {...}

We could also pass some View (for example, Button) to the parameters of the method - automatic type conversion will occur:

You can also specify many identifiers - the selected action will be set for them:

 

Method findById

ButterKnife also provides a static method that simplifies work inside components such as Dialog or View:findById()

With that, our acquaintance with the Butter Knife library has come to an end, so let's similarize the results:

  • work with resources through annotations;
  • replaces cumbersome internal anonymous classes (e.g., click listeners) with a single annotation @OnClick;
  • allows you to generate lists or arrays from the View and apply some actions to them;
  • reduces the amount of required code associated with just one annotation.findViewById()@BindView

My opinion: the library is very interesting and convenient, but in small projects the advantages of using it seem doubtful - I would still use it in medium or even large projects. It seems to me that there it will be able to manifest itself in all its glory: it will significantly reduce the amount of code, make large classes more readable. However, like any other library, it has its own threshold of entry and for novice Android developers it may be difficult at first.

Stay tuned!