Android ButterKnife edit

Binding Views using ButterKnife

we can annotate fields with @BindView and a view ID for Butter Knife to find and automatically cast the corresponding view in our layout.

Binding Views

Binding Views in Activity

class ExampleActivity extends Activity {
  @BindView( TextView title;
  @BindView( TextView subtitle;
  @BindView( TextView footer;

  @Override public void onCreate(Bundle savedInstanceState) {
    // TODO Use fields...

Binding Views in Fragments

public class FancyFragment extends Fragment {
  @BindView( Button button1;
  @BindView( Button button2;
  private Unbinder unbinder;

  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fancy_fragment, container, false);
    unbinder = ButterKnife.bind(this, view);
    // TODO Use fields...
    return view;
  // in fragments or non activity bindings we need to unbind the binding when view is about to be destroyed
    public void onDestroy() {

Binding Views in Dialogs

We can use ButterKnife.findById to find views on a View, Activity, or Dialog. It uses generics to infer the return type and automatically performs the cast.

View view = LayoutInflater.from(context).inflate(R.layout.thing, null);
TextView firstName = ButterKnife.findById(view,;
TextView lastName = ButterKnife.findById(view,;
ImageView photo = ButterKnife.findById(view,;

Binding Views in ViewHolder

static class ViewHolder {
    @BindView( TextView name;
    @BindView( TextView jobTitle;

    public ViewHolder(View view) {
      ButterKnife.bind(this, view);

Binding Resources

Apart from being useful for binding views, one could also use ButterKnife to bind resources such as those defined within strings.xml, drawables.xml, colors.xml, dimens.xml, etc.

public class ExampleActivity extends Activity {

@BindString(R.string.title) String title; @BindDrawable(R.drawable.graphic) Drawable graphic; @BindColor( int red; // int or ColorStateList field @BindDimen(R.dimen.spacer) Float spacer; // int (for pixel size) or float (for exact value) field

@Override public void onCreate(Bundle savedInstanceState) {

// ...




Binding View Lists

You can group multiple views into a List or array. This is very helpful when we need to perform one action on multiple views at once.

@BindViews({,, })
List<EditText> nameViews;

//The apply method allows you to act on all the views in a list at once.
ButterKnife.apply(nameViews, DISABLE);
ButterKnife.apply(nameViews, ENABLED, false);
//We can use Action and Setter interfaces allow specifying simple behavior.
static final ButterKnife.Action<View> DISABLE = new ButterKnife.Action<View>() {
  @Override public void apply(View view, int index) {
static final ButterKnife.Setter<View, Boolean> ENABLED = new ButterKnife.Setter<View, Boolean>() {
  @Override public void set(View view, Boolean value, int index) {

Optional Bindings

By default, both @Bind and listener bindings are required. An exception is thrown if the target view cannot be found. But if we are not sure if a view will be there or not then we can add a @Nullable annotation to fields or the @Optional annotation to methods to suppress this behavior and create an optional binding.

@BindView( TextView mightNotBeThere;

void onMaybeMissingClicked() {
  // TODO ...

Table Of Contents
34 ButterKnife
64 Menu
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
170 XMPP
176 OpenCV
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close