I read about annotations in Android and came across such a lib - ButterKnife.

What is the use of its use? What does it provide besides replacing the findViewById method?

  • one
    The benefits of using ANY library is a simpler solution to certain tasks. Instead of writing a lot of your own code, you use the library and the amount of code decreases at times. What gives one or another library, as a rule, is indicated in the description of this library. For example, on ButterKnife - the link here is present on the project page in GitHub. - pavlofff
  • @pavlofff thanks. - researcher

1 answer 1

There is not only

Button button = (Button) findViewById(R.id.button); 

can be replaced by

 @InjectView(R.id.button) Button mButton; 

But Kolbeki inject.

Say:

 button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // ... } }); 

will turn into:

 @OnClick(R.id.button) public void onButtonClick() { // ... } 

Adapters simplify code. If you wrote your adapter for ListView , you should have written ViewHolder for it. When there is a lot of it, it’s not very nice and convenient to holder elements in the holder . Using ButterKnife is easy:

 static class ViewHolder{ @InjectView(R.id.image_in_item) ImageView image; @InjectView(R.id.textview_in_item) TextView text; public ViewHolder(View view){ ButterKnife.inject(this, view); } } 

If the question is precisely why ButterKnife, and not, say, RoboGuice, then it is smaller. RoboGuice draws a lot of dependencies and slower, for it is in runtime, but ButterKnife does it during compilation.

Yes, and Lieb wrote Jake Wharton. This is the same who wrote ActionBarSherlock and other cool stuff.

UPD: as comrade pavlofff correctly noted, in new versions of the library, @InjectView was replaced with @Bind , plus other changes. It is better to look directly into the library repositories .

  • Thanks for the detailed answer! - researcher
  • about the most important thing - @Bind (and derivatives) did not mention :) - pavlofff
  • @pavlofff I haven't worked with Android for a long time, I don’t remember everything) - Suvitruf
  • one
    In general, the new versions of the @InjectView library @InjectView been replaced by @Bind and somewhat expanded by derivatives. Could you indicate this in the answer, but it is somewhat confusing. - pavlofff
  • one
    @Kostya is very cool, yes) - Suvitruf