Bind a view's children into fields using ButterKnife.bind(this). If you use tags in a layout and inflate in a custom view constructor you can call this immediately after. Alternatively, custom view types inflated from XML can use it in the onFinishInflate()callback.VIEW LISTSYou can group multiple views into a List or array.
@Bind({ R.id.first_name, R.id.middle_name, R.id.last_name })List 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);
Action and Setter interfaces allow specifying simple behavior.
static final ButterKnife.Action DISABLE = new ButterKnife.Action() { @Override public void apply(View view, int index) { view.setEnabled(false); }};static final ButterKnife.Setter ENABLED = new ButterKnife.Setter() { @Override public void set(View view, Boolean value, int index) { view.setEnabled(value); }};An Android Property can also be used with the apply method.
ButterKnife.apply(nameViews, View.ALPHA, 0.0f);
LISTENER BINDINGListeners can also automatically be configured onto methods.
@OnClick(R.id.submit)public void submit(View view) { // TODO submit data to server...}All arguments to the listener method are optional.
@OnClick(R.id.submit)public void submit() { // TODO submit data to server...}Define a specific type and it will automatically be cast.
@OnClick(R.id.submit)public void sayHi(Button button) { button.setText(Hello!);}Specify multiple IDs in a single binding for common event handling.
@OnClick({ R.id.door1, R.id.door2, R.id.door3 })public void pickDoor(DoorView door) { if (door.hasPrizeBehind()) { Toast.makeText(this, You win!, LENGTH_SHORT).show(); } else { Toast.makeText(this, Try again, LENGTH_SHORT).show(); }}Custom views can bind to their own listeners by not specifying an ID.
public class FancyButton extends Button { @OnClick public void onClick() { // TODO do something! }}BINDING RESETFragments have a different view lifecycle than activities. When binding a fragment inonCreateView, set the views to null in onDestroyView. Butter Knife has an unbind method to do this automatically.
public class FancyFragment extends Fragment { @Bind(R.id.button1) Button button1; @Bind(R.id.button2) Button button2; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fancy_fragment, container, false); ButterKnife.bind(this, view); // TODO Use fields... return view; } @Override public void onDestroyView() { super.onDestroyView(); ButterKnife.unbind(this); }}OPTIONAL BINDINGSBy default, both @Bind and listener bindings are required. An exception will be thrown if the target view cannot be found.
To suppress this behavior and create an optional binding, add a @Nullable annotation to the field or method.
Note: Any annotation named @Nullable can be used for this purpose. It is encouraged to use the@Nullable annotation from Android's support-annotations library, see Android Tools Project.
@Nullable @Bind(R.id.might_not_be_there) TextView mightNotBeThere;@Nullable @OnClick(R.id.maybe_missing) void onMaybeMissingClicked() { // TODO ...}MULTI-METHOD LISTENERSMethod annotations whose corresponding listener has multiple callbacks can be used to bind to any one of them. Each annotation has a default callback that it binds to. Specify an alternate using the callback parameter.
@OnItemSelected(R.id.list_view)void onItemSelected(int position) { // TODO ...}@OnItemSelected(value = R.id.maybe_missing, callback = NOTHING_SELECTED)void onNothingSelected() { // TODO ...}BONUSAlso included are findById methods which simplify code that still has 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);
Add a static import for ButterKnife.findById and enjoy even more fun.
DownloadButter Knife v7.0.1 JAR
The source code to the library and sample application as well as this website is available on GitHub. The Javadoc is also available to browse.
MAVENIf you are using Maven for compilation you can declare the library as a dependency.
com.jakewharton butterknife 7.0.1
GRADLEcompile 'com.jakewharton:butterknife:7.0.1'
Be sure to suppress this lint warning in your build.gradle.
lintOptions { disable 'InvalidPackage'}Some configurations may also require additional exclusions.
packagingOptions { exclude 'META-INF/services/javax.annotation.processing.Processor'}IDE CONFIGURATIONSome IDEs require additional configuration in order to enable annotation processing.
- IntelliJ IDEA — If your project uses an external configuration (like a Maven
pom.xml) then annotation processing should just work. If not, try manual configuration.
- Eclipse — Set up manual configuration.PROGUARD
Butter Knife generates and uses classes dynamically which means that static analysis tools like ProGuard may think they are unused. In order to prevent them from being removed, explicitly mark them to be kept. To prevent ProGuard renaming classes that use @Bind on a member field thekeepclasseswithmembernames option is used.
-keep class butterknife.** { *; }-dontwarn butterknife.internal.**-keep class **$$ViewBinder { *; }-keepclasseswithmembernames class * { @butterknife.* ;}-keepclasseswithmembernames class * { @butterknife.* ;}