Butterknife is a view binding tool that uses annotations to generate boilerplate code for us. This tool is developed by Jake Wharton at Square and is essentially used to save typing repetitive lines of code like findViewById(R.id.view) when dealing with views thus making our code look a lot cleaner.
To be clear, Butterknife is not a dependency injection library. Butterknife injects code at compile time. It is very similar to the work done by Android Annotations.
Field and method binding for Android views which uses annotation processing to generate boilerplate code for you.
Eliminate findViewById calls by using @BindView on fields.
Group multiple views in a list or array. Operate on all of them at once with actions, setters, or properties.
Eliminate anonymous inner-classes for listeners by annotating methods with @OnClick and others.
Eliminate resource lookups by using resource annotations on fields.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Android Studio ButterKnife Plugin
Android ButterKnife Zelezny
Plugin for generating ButterKnife injections from selected layout XMLs in activities/fragments/adapters.
Note : Make sure that you make the right click for your_xml_layou(R.layout.your_xml_layou) else the Generate menu will not contain Butterknife injector option.
All arguments to the listener method are optional:
Specific type will be automatically casted:
Multiple IDs in a single binding for common event handling:
Custom Views can bind to their own listeners by not specifying an ID:
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 in Activity
Binding Views in Fragments
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.
Binding Views in ViewHolder
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.
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.
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.
Configuring ButterKnife in your project
Configure your project-level build.gradle to include the android-apt plugin:
Then, apply the android-apt plugin in your module-level build.gradle and add the ButterKnife dependencies:
Note: If you are using the new Jack compiler with version 2.2.0 or newer you do not need the android-apt plugin and can instead replace apt with annotationProcessor when declaring the compiler dependency.
In order to use ButterKnife annotations you shouldn't forget about binding them in onCreate() of your Activities or onCreateView() of your Fragments:
Below are the additional steps you'd have to take to use ButterKnife in a library project
To use ButterKnife in a library project, add the plugin to your project-level build.gradle:
…and then apply to your module by adding these lines on the top of your library-level build.gradle:
Now make sure you use R2 instead of R inside all ButterKnife annotations.
Unbinding views in ButterKnife
Fragments have a different view lifecycle than activities. When binding a fragment in onCreateView, set the views to null in onDestroyView. Butter Knife returns an Unbinder instance when you call bind to do this for you. Call its unbind method in the appropriate lifecycle callback.
Note: Calling unbind() in onDestroyView() is not required, but
recommended as it saves quite a bit of memory if your app has a large
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0