-5

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?

Morl
  • 37
  • 2
  • 7

3 Answers3

0

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;
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Remario
  • 3,813
  • 2
  • 18
  • 25
0

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.

Dan Davis
  • 524
  • 5
  • 9
-1

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'
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
sadat
  • 4,004
  • 2
  • 29
  • 49