Butter Knife - Free

  • Butter Knife, Butter Knife Library, Android Library
  • Description

  • change log

  • faq

  • Comments

  • Introduction Annotate fields with @InjectView and a view ID for Butter Knife to find and automatically cast the corresponding view in your layout.
    class ExampleActivity extends Activity {
      @InjectView(R.id.title) TextView title;
      @InjectView(R.id.subtitle) TextView subtitle;
      @InjectView(R.id.footer) TextView footer;
    
      @Override public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_activity);
        ButterKnife.inject(this);
        // TODO Use "injected" views...
      }
    }
    
    Instead of slow reflection, code is generated to perform the view look-ups. Calling inject delegates to this generated code that you can see and debug. The generated code for the above example is roughly equivalent to the following:
    public void inject(ExampleActivity activity) {
      activity.subtitle = (android.widget.TextView) activity.findViewById(2130968578);
      activity.footer = (android.widget.TextView) activity.findViewById(2130968579);
      activity.title = (android.widget.TextView) activity.findViewById(2130968577);
    }
    
    Non-Activity Injection You can also perform injection on arbitrary objects by supplying your own view root.
    public class FancyFragment extends Fragment {
      @InjectView(R.id.button1) Button button1;
      @InjectView(R.id.button2) Button button2;
    
      @Override View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fancy_fragment, container, false);
        ButterKnife.inject(this, view);
        // TODO Use "injected" views...
        return view;
      }
    }
    
    Another use is simplifying the view holder pattern inside of a list adapter.
    public class MyAdapter extends BaseAdapter {
      @Override public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder;
        if (view != null) {
          holder = (ViewHolder) view.getTag();
        } else {
          view = inflater.inflate(R.layout.whatever, parent, false);
          holder = new ViewHolder(view);
          view.setTag(holder);
        }
    
        holder.name.setText("John Doe");
        // etc...
    
        return view;
      }
    
      static class ViewHolder {
        @InjectView(R.id.title) TextView name;
        @InjectView(R.id.job_title) TextView jobTitle;
    
        public ViewHolder(View view) {
          ButterKnife.inject(this, view);
        }
      }
    }
    
    You can see this implementation in action in the provided sample. Calls to ButterKnife.inject can be made anywhere you would otherwise put findViewById calls. Other provided injection APIs: Inject arbitrary objects using an activity as the view root. If you use a pattern like MVC you can inject the controller using its activity with ButterKnife.inject(this, activity). Inject a view's children into fields using ButterKnife.inject(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 Lists You can group multiple views into a List or array. @InjectViews({ 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 Action DISABLE = new Action<>() {
      @Override public void apply(View view, int index) {
        view.setEnabled(false);
      }
    }
    static final Setter ENABLED = new 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); Listener Injection Listeners 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!
      }
    }
    
    Injection Reset Fragments have a different view lifecycle than activities. When injecting a fragment in onCreateView, set the views to null in onDestroyView. Butter Knife has a reset method to do this automatically.
    public class FancyFragment extends Fragment {
      @InjectView(R.id.button1) Button button1;
      @InjectView(R.id.button2) Button button2;
    
      @Override View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fancy_fragment, container, false);
        ButterKnife.inject(this, view);
        // TODO Use "injected" views...
        return view;
      }
    
      @Override void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
      }
    }
    
    Optional Injections By default, both @InjectView and listener injections are required. An exception will be thrown if the target view cannot be found. To suppress this behavior and create an optional injection, add the @Optional annotation to the field or method.
    @Optional @InjectView(R.id.might_not_be_there) TextView mightNotBeThere;
    
    @Optional @OnClick(R.id.maybe_missing) void onMaybeMissingClicked() {
      // TODO ...
    }
    
    Multi-Method Listeners Method 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 ...
    }
    
    Bonus Also 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);
    
    There is no Features content
  • There is no Related Products
  • There is no Change Log content
  • There is no FAQ