Stay on Top with latest happenings: Subscribe Now

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.

Benefits:

  • 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:

@bindView(R.id._view)
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(R.string.name) String name;
  @BindDrawable(R.drawable.user_img) Drawableuser_img;
  @BindColor(R.color.green) 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(R.id.name) TextView name;
  @BindView(R.id.address) TextView address;
  @BindView(R.id.email) TextView email;

  @Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_activity);
ButterKnife.bind(this);

  }
}

Non-Activity Binding

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

public class MyFragment extends Fragment {
  @BindView(R.id.button) Button button1;
  @BindView(R.id.image) 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.

@OnClick(R.id.done)
public void done(View view) {
// TODO perform your action...  }
@OnClick(R.id.display_data)
public void DisplayData() {
  // TODO perform your action...
}

Conclusion:

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
Shares