Ever since I started mobile development I’ve been using ButterKnife, A must for every Android project for me. I hope most of you’re familiar with it… and just in case you’re not, check it out on Github Pages.
I am using ButterKnife since last two years and have seen many projects on GitHub that use ButterKnife but one thing I noticed is that very few of those projects actually make full use of this library. Even I wasn’t using its awesome features until recently. Most of us use it only for binding views and for setting clicks listeners to remove boilerplate code from our classes. I’ve seen many people on StackOverflow using @OnClick
from Butterknife library and at the same time, they use setOnLongClickListener()
in the same activity. Why don’t they use @OnLongClick
, @OnTextChanged
or @BindBitmap
? At the time of writing ButterKnife 8.8.1 has 24 different annotations that we can use. See
We can divide ButterKnife into three different types of binding. Resources Binding,View binding and Listener Binding. Let’s dive into Resource Binding and check different annotations it offers…
Resource Binding
@BindAnim
Bind a field to the specified animation resource ID.
Usage:
BindAnim(R.anim.fade_in) Animation fadeIn;
Voila, now you have your animation object which you can use just like any other animation object
@BindArray
This annotation supports 4 array data types — int, CharSequence, String, and TypedArray.
@BindArray(R.array.countries) String[] countries; @BindArray(R.array.icons) TypedArray icons;
@BindBitmap
@BindBitmap(R.drawable.logo) Bitmap logo;
@BindBool
@BindBool(R.bool.is_tablet) boolean isTablet;
@BindColor
@BindColor(R.color.background_green) int green;
@BindDimen
@BindDimen(R.dimen.horizontal_gap) float gap;
@BindDrawable
@BindDrawable(R.drawable.placeholder) Drawable placeholder; @BindDrawable(value=R.drawable.holder, tint=R.attr.red) Drawable tinted;
@BindFloat & @BindInt
@BindFloat(R.dimen.image_ratio) float imageRatio; @BindInt(R.int.columns) int columns;
@BindString
@BindString(R.string.username) String username;
@BindFont
Those who are compiling their projects on Android Oreo will find this annotation very helpful. We can now request font from font provider and bind them with @BindFont
@BindFont(R.font.brandon_text_medium) Typeface mBoldTypeface;
Note: This requires support libraries 26.0.0-beta1 or newer
View binding
@BindView
Android developers use it so much that we can rename this library as Android-Bind-View-Library. It binds a field to the view for the specified ID. The view will automatically be cast to the field type.
@BindView(R.id.title) TextView title;
@BindViews
Now this one’s interesting because it has an extra ‘s’. This thing can group multiple views into a List or Array.
@BindViews({R.id.etFirstName, R.id.etMiddleName, R.id.etLastName }) List<EditText> nameList;
Now suppose you want to perform some task on one of the views so you have to iterate through them to perform operations… huh ? that’s where Action, Setters and Properties of ButterKnife come into play. We’ll talk about them in next part. We’ll also take a look at Listener binding, @Optional annotation, Multi-Method callbacks like afterTextChanged
, beforeTextChanged
and some other cool stuff.