Binding Views using ButterKnife

suggest change

Annotate fields with @BindView and a view id. Butter Knife finds the view and casts it.

Binding Views

Binding Views in Activity

public 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...
  }
}

Binding Views in Fragments

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

  @Override 
  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;
  }
  
  // Unbind in onDestroyView. The view can be destroyed while the fragment instance remains.
  @Override
  public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();
  }
}

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, R.id.first_name);
TextView lastName = ButterKnife.findById(view, R.id.last_name);
ImageView photo = ButterKnife.findById(view, R.id.photo);

Binding Views in ViewHolder

static class ViewHolder {
    @BindView(R.id.title) TextView name;
    @BindView(R.id.job_title) TextView jobTitle;

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

Binding Resources

Butter Knife can also bind resources from strings.xml, drawables, colors.xml, dimens.xml, and similar files.

public class ExampleActivity extends Activity {
    @BindString(R.string.title) String title;
    @BindDrawable(R.drawable.graphic) Drawable graphic;
    @BindColor(R.color.red) int red; // int or ColorStateList field
    @BindDimen(R.dimen.spacer) float spacer; // int (for pixel size) or float (for exact value)

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // ...
        ButterKnife.bind(this);
    }
}

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({ R.id.first_name, R.id.middle_name, R.id.last_name })
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) {
    view.setEnabled(false);
  }
};
static final ButterKnife.Setter<View, Boolean> ENABLED = new ButterKnife.Setter<View, Boolean>() {
  @Override public void set(View view, Boolean value, int index) {
    view.setEnabled(value);
  }
};

Optional Bindings

By default, @BindView and listener bindings are required. An exception is thrown if the target view cannot be found. If the view might be absent, add @Nullable to the field or @Optional to the method.

@Nullable 
@BindView(R.id.might_not_be_there) TextView mightNotBeThere;

@Optional 
@OnClick(R.id.maybe_missing) 
void onMaybeMissingClicked() {
  // TODO ...
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents