ButterKnife : The Right Way [Part 1]

Ever since I started mobile development I used many libraries some of them are evergreen, A must for every Android project of mine and one of them is ButterKnife. I hope most of you’re familiar with it… and just in case you’re not, check it out on Github.

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 use of this library. Even I wasn’t using its awesome features until recently. Most of us use it only for binding view (the findViewById Phobia) and for setting clicks (The SetOnClickListener Phobia ) on views. I’ve seen many people on StackOverflow using @OnClick from Butterknife at the same time using setOnLongClickListener() to their buttons on the same activity. So what about other listeners? Bindings other than BindView ? Like @OnTextChanged or @BindBitmap ? At the time of writing ButterKnife 8.8.1 has 24 different annotations that we can use. See

Let’s start with resource binding annotations that we use in our projects and save ourselves from writing boilerplate code. We can divide ButterKnife into three different types of binding. Resources Binding, View binding, Listener Binding


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 : You can bind Color and ColorStateList with this annotation
@BindColor(R.color.background_green) int green;

@BindDimen: You can bind int and float type of dimension
@BindDimen(R.dimen.horizontal_gap) float gap;

@BindDrawable : Recently butterknife got an update to support tint color with @BindDrawable annotation 
@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;

@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

@BindString :
@BindString(R.string.username) String username;


View binding -
@BindView : This annotation is the reason I am writing this article on Medium. 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 view so you have to iterate through them to perform operations. BUT WAIT, 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 into LISTENER BINDING, @Optional annotation, Multi-Method callbacks like afterTextChanged, beforeTextChanged and some other cool stuff.

Update: Check Out part two.

“Remember: A butter knife is like a dagger only infinitely less sharp.” ~ Jake Wharton