Tips to use ButterKnife – Dependency Injection in Android

Tips to use ButterKnife – Dependency Injection in Android

Dependency Injection

What is Dependency Injection?

It’s a design pattern in Android that allows writing codes for low coupling and also makes it possible to change them any time.


  • Promotes loose coupling between classes and subsystems
  • Easy to use and modify the components
  • Testing the functionalities is easy too

Libraries available in Android:

  • ButterKnife
  • Roboguice
  • Android Annotations
  • Dagger 2

Dependency Injection with ButterKnife :

ButterKnife helps to represent the views from an activity or fragment and also helps in handling various events like onClick(), onLongClick() etc. through annotations.

How to Use ButterKnife Library?

Step 1: AddDependency

Add the following dependency to our build.gradle.

compile ‘com.jakewharton:butterknife:6.1.0’

Step 2 : Use Annotations

Instead of using findViewById() method, add @InjectView annotation before the declaration of a variable.

For example:

TextView _view;

Step 3 : Implementation

Resource Binding

Bind pre-defined resources with @BindBool, @BindColor, @BindDimen, @BindDrawable, @BindInt, @BindString, which binds an R.bool ID (or your specified type) to its corresponding field.

class TestActivity extends Activity {
  @BindString( String name;
  @BindDrawable(R.drawable.user_img) Drawableuser_img;
  @BindColor( int green; // int or ColorStateList field
  @BindDimen(R.dimen.latitude) Float latitude; 

Views Binding

Multiple views can be grouped into a list or array.

class TestActivity extends Activity {
  @BindView( TextView name;
  @BindView( TextView address;
  @BindView( TextView email;

  @Override public void onCreate(Bundle savedInstanceState) {


Non-Activity Binding

Binding can be done on arbitrary objects by supplying the view root.

public class MyFragment extends Fragment {
  @BindView( Button button1;
  @BindView( ImageView image;

  @Override public View onCreateView(LayoutInflaterinflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.my_fragment, container, false);
ButterKnife.bind(this, view);
      return view;

Listener Binding

Listeners can also automatically be configured onto methods.

public void done(View view) {
// TODO perform your action...  }
public void DisplayData() {
  // TODO perform your action...


The dependency injection (DI) has become an increasingly popular tool in Android App development, and for good reasons. Injections reduce the amount of coding you perform and hence, debugging too, facilitating smoother development process and creation of better apps.

While it may be tempting to toss in dependencies to a variety of libraries, it’s also important to keep in mind the potential toll that dependency injections can have on your app’s performance.

Anything missing here? I’d appreciate if you can share your thoughts below…

Ankeet P

    Contact Us

    We’d love to help & work with you

    When do you want to start ?

    Enter your email address to stay up to date with the latest news.
    Holler Box

    Exit pop up

    Sad to see you leaving early...

    From "Aha" to "Oh shit" we are sharing everything on our journey.
    Enter your email address to stay up to date with the latest news.
    Holler Box