Tips To Use ButterKnife – Dependency Injection In Android

Tips To Use ButterKnife - Dependency Injection In Android
Social sharing
Contents

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: Add Dependency

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…

Your recently viewed posts:

Netrananda M - Post Author

SEO, Content Marketing, Digital Marketing, Inbound and Growth Marketing Professional focused on SaaS product marketing and service promotion Andolasoft.

    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

    Orange Exit pop up

    Subscribe for the latest
    trends in web and
    mobile app development
    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