Android View Injection Tool Butterknife Examples

Butterknife is a view injection tool which helps you to reduce your line of codes by getting rid of those boilerplate code that you needed in order to create the views. By using Butterknife, you use the @Bind annotation to bind the views and it will do find view by id and view casting works for you.

Without Butterknife

public class MyActivity extends Activity {
  TextView tvTitle;
  ImageView ivPoster;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_activity);
	tvTitle = (TextView) findViewById(R.id.tv_title);
	ivPoster = (TextView) findViewById(R.id.iv_poster);
  }
}

With Butterknife, imagine if you have a complex layout with 20+ views, Butterknife will really make your code to look nicer.

public class MyActivity extends Activity {
  @BindView(R.id.tv_title) TextView tvTitle;
  @BindView(R.id.iv_poster) ImageView ivPoster;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_activity);
    ButterKnife.bind(this);
    // Now you can start use the fields just like the way you use after do you findViewById() and cast it to the view type
  }
}

To include ButterKnife in your android project, add the following in you app build gradle file’s dependencies tag

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

Using Butterknife in fragment class, remember to unbind it onDestroyView.

public class MyFragment extends Fragment {
  @BindView(R.id.tv_title) TextView tvTitle;
  @BindView(R.id.iv_poster) ImageView ivPoster;

  @Override 
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.my_fragment, container, false);
    ButterKnife.bind(this, view);

    // Start to use the fields
    return view;
  }

    @Override 
  	public void onDestroyView() {
     	super.onDestroyView();
    	ButterKnife.unbind(this);
    }
}

Using Butterknife in recycler view adapter ViewHolder.

public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> {
	
	// other required recycler view adapter methods ....

	public static class MyViewHolder extends RecyclerView.ViewHolder {
  		@BindView(R.id.tv_title) TextView tvTitle;
  		@BindView(R.id.iv_poster) ImageView ivPoster;

  		MyViewHolder(Viwe v) {
  			super(v);
  			ButterKnife.bind(this, v);
  		}
	}

}

Using Butterknife for button onClick.

public class MyActivity extends Activity {

  @Override 
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_activity);
    ButterKnife.bind(this);
  }

  @OnClick(R.id.btn_click)
  public void btnClicked(View v) {
  	// do something when the button is clicked
  }  
}

source

Search within Codexpedia

Custom Search

Search the entire web

Custom Search