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:

@Override 
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
ButterKnife.bind(this); // bind butter knife after setContectView(..)
//Now you can access all view after bind your activity with butterknife
}

2. bind butterknife fragment:

@Override
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>() {
    @Override
    public void apply(@NonNull TextView view, int index) {
        if(index % 2 == 0 ){
            view.setTextColor(pinkColor);
        } else {
            view.setTextColor(blackColor);
        }
    }
};

Butterknife setter

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

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

@OnClick(R.id.button1) 
public void sayHello(){ 
    Toast.makeText(this,helloText,Toast.LENGTH_SHORT).show(); 
}

Single view button click event with view param

@OnClick(R.id.button1) 
public void sayHello(View view){
    Toast.makeText(this,helloText,Toast.LENGTH_SHORT).show();
}

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:
            break; 
        case R.id.button2:
            break; 
    } 
}

OnItemSelected Event binding on spinner:

@BindView(R.id.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); 
spinner.setAdapter(arrayAdapter);
@OnItemSelected(R.id.spinner) 
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 *

Back to Top