I am currently working in a project where Butterknife plugin is being used, i noticed something like @BindView(R.id.something). How do we use butterknife plugin in an app?
-
read the docs!! [link](http://jakewharton.github.io/butterknife/) – El Don Feb 28 '17 at 03:41
-
@Morl or be specific, what will you use it for – El Don Feb 28 '17 at 03:42
-
You're already working in an app using Butterknife but you haven't even bothered to learn the technologies used? More astounded every day – Nick Cardoso Feb 28 '17 at 03:44
-
1I request you to google before posting questions. Wharton himself covered each scenario. – rohitanand Feb 28 '17 at 03:59
-
If you go through its documentation properly.. you will get to know how to use it .. – Riten Feb 28 '17 at 04:18
3 Answers
It binds android views to methods and callbacks.
It does the same as View.findViewById(R.id.view_name)
.
To use the library , you must initialize it, in onCreate
:
ButterKnife.bind(this);
Then, for example declare:
ImageView image = null;
instead of
image = (ImageView)parent.findViewById(R.id.view);
You can do this instead:
@BindView(R.id.view) ImageView image;

- 3,747
- 4
- 27
- 53

- 3,813
- 2
- 18
- 25
Quoted from http://jakewharton.github.io/butterknife/
Annotate fields with @BindView and a view ID for Butter Knife to find and automatically cast the corresponding view in your layout.
class ExampleActivity extends Activity {
@BindView(R.id.title) TextView title;
@BindView(R.id.subtitle) TextView subtitle;
@BindView(R.id.footer) TextView footer;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.bind(this);
// TODO Use fields...
}
}
ID your elements in R.layout.simple_activity and then use @BindView to associate, aka bind, those views to variables in your activity. So, rather than having to use findViewById() repeatedly, ButterKnife is doing all this work for you. Thus, title, subtitle, footer will point at those elements in your layout.

- 524
- 5
- 9
Have a look into the documentation:
http://jakewharton.github.io/butterknife/
You have to add the following lines in your gradle:
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'

- 3,747
- 4
- 27
- 53

- 4,004
- 2
- 29
- 49