Butterknife Android Studio with Example [Latest]

ButterKnife Android is a view binding library for Android that uses annotation to generate boilerplate code for us. It makes your code less and more clear. It is time-saving to write repetitive lines of code.To avoid writing repetitive code just like `findViewById(R.id.yourview)` butter knife helps you to binds fields, method and views for you.

velmm download codegithub

ButterKnife Setup:
Add dependency to your app level build.gradle file:

 compile 'com.jakewharton:butterknife:8.8.1'
 annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

How to use Butterknife

1. bind butterknife with activity:

public void onCreate(Bundle savedInstanceState) {
ButterKnife.bind(this); // bind butter knife after setContectView(..)
//Now you can access all view after bind your activity with butterknife

2. bind butterknife fragment:

public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater
.inflate(R.layout.fragment_somelayout, container, false);
ButterKnife.bind(getActivity(), rootView); 
//need to specify sources of view
return rootView;

Usage of Butterknife

view binding

@BindView(R.id.button1) Button button1;
@BindView(R.id.spinner) Spinner spinner;

Resource binding

@BindColor(R.color.pink) int pinkColor; 
@BindColor(R.color.black) int blackColor; 
@BindString(R.string.hello) String helloText;

Bind multiple views into the list:

@BindViews({R.id.textView1,R.id.textView2}) List<TextView> textViews;

Butterknife action

public final ButterKnife.Action<TextView> SET_DEFAULT_COLOR = new ButterKnife.Action<TextView>() {
    public void apply(@NonNull TextView view, int index) {
        if(index % 2 == 0 ){
        } else {

Butterknife setter

public final ButterKnife.Setter<View,Boolean> ENABLE = new ButterKnife.Setter<View, Boolean>() { 
    public void set(@NonNull View view, Boolean value, int index) { 

Apply ACTION & SETTER to multiple views

ButterKnife.apply(textViews,SET_DEFAULT_COLOR); // setup action to the views

Event binding:
Instead of writing setOnClickListener method for every view, use @OnClick annotation provide click event for the single or multiple views.

Single view button click event

public void sayHello(){ 

Single view button click event with view param

public void sayHello(View view){

Multiple Button click event, must have to include view here to get onClickEvent

@OnClick({ R.id.button1, R.id.button2})
public void setViewOnClickEvent(View view) {
    switch(view.getId()) { 
        case R.id.button1:
        case R.id.button2:

OnItemSelected Event binding on spinner:

Spinner spinner;
List<String> stringList = new ArrayList<>(); 
stringList.add("Item 1"); 
stringList.add("Item 2"); 
stringList.add("Item 3"); 
ArrayAdapter arrayAdapter = new ArrayAdapter(this,R.layout.support_simple_spinner_dropdown_item,stringList); 
public void onItemSelected(View view,int position,long i){ 
    Toast.makeText(this,"Item Selected = "+stringList.get(position).toString(),Toast.LENGTH_SHORT).show(); 

List of event listener by butter knife

Butterknife Annotation | Interface

@OnEditorAction | OnEditorActionListener

@OnFocusChange | View.OnFocusChangeListener

@OnTouch | View.OnTouchListener

@OnCheckedChange | CompoundButton.OnCheckedChangeListener

@OnClick | butterknife.internal.DebouncingOnClickListener

@OnItemLongClick | AdapterView.OnItemLongClickListener

@OnItemClick | AdapterView.OnItemClickListener

@OnLongClick | View.OnLongClickListener

Viewbinding in Adapter 

Related: Cardview Android Material Design with Example [Download]

Related: Android Recyclerview material design With Example [Download]

Related: Retrofit Android Example With Recyclerview [Download]


Leave a Reply

Your email address will not be published. Required fields are marked *