public final class ButterKnife
extends java.lang.Object
Finding views from your activity is as easy as:
public class ExampleActivity extends Activity {
@BindView(R.id.title) EditText titleView;
@BindView(R.id.subtitle) EditText subtitleView;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.example_activity);
ButterKnife.bind(this);
}
}
Binding can be performed directly on an activity, a
view, or a dialog. Alternate objects to
bind can be specified along with an activity,
view, or
dialog.
Group multiple views together into a List or array.
@BindView({R.id.first_name, R.id.middle_name, R.id.last_name})
List nameViews;
There are three convenience methods for working with view collections:
apply(List, Action) – Applies an action to each view.apply(List, Setter, Object) – Applies a setter value to each view.apply(List, Property, Object) – Applies a property value to each view.To bind listeners to your views you can annotate your methods:
@OnClick(R.id.submit) void onSubmit() {
// React to button click.
}
Any number of parameters from the listener may be used on the method.
@OnItemClick(R.id.tweet_list) void onTweetClicked(int position) {
// React to tweet click.
}
Be default, views are required to be present in the layout for both field and method bindings.
If a view is optional add a @Nullable annotation for fields (such as the one in the
support-annotations library)
or the @Optional annotation for methods.
@Nullable @BindView(R.id.title) TextView subtitleView;
Resources can also be bound to fields to simplify programmatically working with views:
@BindBool(R.bool.is_tablet) boolean isTablet;
@BindInt(R.integer.columns) int columns;
@BindColor(R.color.error_red) int errorRed;
| Modifier and Type | Class and Description |
|---|---|
static interface |
ButterKnife.Action<T extends android.view.View>
An action that can be applied to a list of views.
|
static interface |
ButterKnife.Setter<T extends android.view.View,V>
A setter that can apply a value to a list of views.
|
| Modifier and Type | Method and Description |
|---|---|
static <T extends android.view.View> |
apply(java.util.List<T> list,
ButterKnife.Action<? super T>... actions)
Apply the specified
actions across the list of views. |
static <T extends android.view.View> |
apply(java.util.List<T> list,
ButterKnife.Action<? super T> action)
Apply the specified
action across the list of views. |
static <T extends android.view.View,V> |
apply(java.util.List<T> list,
ButterKnife.Setter<? super T,V> setter,
V value)
Set the
value using the specified setter across the list of views. |
static <T extends android.view.View,V> |
apply(java.util.List<T> list,
android.util.Property<? super T,V> setter,
V value)
Apply the specified
value across the list of views using the property. |
static <T extends android.view.View> |
apply(T[] array,
ButterKnife.Action<? super T>... actions)
Apply the specified
actions across the array of views. |
static <T extends android.view.View> |
apply(T[] array,
ButterKnife.Action<? super T> action)
Apply the specified
action across the array of views. |
static <T extends android.view.View,V> |
apply(T[] array,
ButterKnife.Setter<? super T,V> setter,
V value)
Set the
value using the specified setter across the array of views. |
static <T extends android.view.View,V> |
apply(T[] array,
android.util.Property<? super T,V> setter,
V value)
Apply the specified
value across the array of views using the property. |
static <T extends android.view.View> |
apply(T view,
ButterKnife.Action<? super T>... actions)
Apply
actions to view. |
static <T extends android.view.View> |
apply(T view,
ButterKnife.Action<? super T> action)
Apply
action to view. |
static <T extends android.view.View,V> |
apply(T view,
ButterKnife.Setter<? super T,V> setter,
V value)
Set
value on view using setter. |
static <T extends android.view.View,V> |
apply(T view,
android.util.Property<? super T,V> setter,
V value)
Apply
value to view using property. |
static Unbinder |
bind(android.app.Activity target)
BindView annotated fields and methods in the specified
Activity. |
static Unbinder |
bind(android.app.Dialog target)
BindView annotated fields and methods in the specified
Dialog. |
static Unbinder |
bind(java.lang.Object target,
android.app.Activity source)
BindView annotated fields and methods in the specified
target using the source
Activity as the view root. |
static Unbinder |
bind(java.lang.Object target,
android.app.Dialog source)
BindView annotated fields and methods in the specified
target using the source
Dialog as the view root. |
static Unbinder |
bind(java.lang.Object target,
android.view.View source)
BindView annotated fields and methods in the specified
target using the source
View as the view root. |
static Unbinder |
bind(android.view.View target)
BindView annotated fields and methods in the specified
View. |
static <T extends android.view.View> |
findById(android.app.Activity activity,
int id)
Simpler version of
Activity.findViewById(int) which infers the target type. |
static <T extends android.view.View> |
findById(android.app.Dialog dialog,
int id)
Simpler version of
Dialog.findViewById(int) which infers the target type. |
static <T extends android.view.View> |
findById(android.view.View view,
int id)
Simpler version of
View.findViewById(int) which infers the target type. |
static void |
setDebug(boolean debug)
Control whether debug logging is enabled.
|
public static void setDebug(boolean debug)
public static Unbinder bind(android.app.Activity target)
Activity. The current content
view is used as the view root.target - Target activity for view binding.public static Unbinder bind(android.view.View target)
View. The view and its children
are used as the view root.target - Target view for view binding.public static Unbinder bind(android.app.Dialog target)
Dialog. The current content
view is used as the view root.target - Target dialog for view binding.public static Unbinder bind(java.lang.Object target, android.app.Activity source)
target using the source
Activity as the view root.target - Target class for view binding.source - Activity on which IDs will be looked up.public static Unbinder bind(java.lang.Object target, android.view.View source)
target using the source
View as the view root.target - Target class for view binding.source - View root on which IDs will be looked up.public static Unbinder bind(java.lang.Object target, android.app.Dialog source)
target using the source
Dialog as the view root.target - Target class for view binding.source - Dialog on which IDs will be looked up.@SafeVarargs
public static <T extends android.view.View> void apply(java.util.List<T> list,
ButterKnife.Action<? super T>... actions)
actions across the list of views.@SafeVarargs
public static <T extends android.view.View> void apply(T[] array,
ButterKnife.Action<? super T>... actions)
actions across the array of views.public static <T extends android.view.View> void apply(java.util.List<T> list,
ButterKnife.Action<? super T> action)
action across the list of views.public static <T extends android.view.View> void apply(T[] array,
ButterKnife.Action<? super T> action)
action across the array of views.@SafeVarargs
public static <T extends android.view.View> void apply(T view,
ButterKnife.Action<? super T>... actions)
actions to view.public static <T extends android.view.View> void apply(T view,
ButterKnife.Action<? super T> action)
action to view.public static <T extends android.view.View,V> void apply(java.util.List<T> list,
ButterKnife.Setter<? super T,V> setter,
V value)
value using the specified setter across the list of views.public static <T extends android.view.View,V> void apply(T[] array,
ButterKnife.Setter<? super T,V> setter,
V value)
value using the specified setter across the array of views.public static <T extends android.view.View,V> void apply(T view,
ButterKnife.Setter<? super T,V> setter,
V value)
value on view using setter.public static <T extends android.view.View,V> void apply(java.util.List<T> list,
android.util.Property<? super T,V> setter,
V value)
value across the list of views using the property.public static <T extends android.view.View,V> void apply(T[] array,
android.util.Property<? super T,V> setter,
V value)
value across the array of views using the property.public static <T extends android.view.View,V> void apply(T view,
android.util.Property<? super T,V> setter,
V value)
value to view using property.public static <T extends android.view.View> T findById(android.view.View view,
int id)
View.findViewById(int) which infers the target type.public static <T extends android.view.View> T findById(android.app.Activity activity,
int id)
Activity.findViewById(int) which infers the target type.public static <T extends android.view.View> T findById(android.app.Dialog dialog,
int id)
Dialog.findViewById(int) which infers the target type.