Friday, 16 September 2016

ButterKnife 8.2.1 for Field and method binding using annotations in android

Introduction 


       Sometimes developers have to deal with redundant coding that is visually unattractive and frustrating. In this article, I will introduce an injection library for Android development that can help create more beautiful code and recent updates introduced with version 8.

Android Butter Knife  is an open source view “injection” library for Android created by Jake Wharton.

    Butter Knife is small, simple and lightweight, and it makes life as a developer easier. It allows developers to perform injection on arbitrary objects,views and OnClickListeners so they can focus on writing useful code. Butter Knife enables focus on logic instead of glue code and reduces development time by reducing redundant coding.

  • 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.

Features

  1. VIEW BINDING
  2. RESOURCE BINDING
  3. NON-ACTIVITY BINDING
  4. VIEW LISTS
  5. LISTENER BINDING
  6. BINDING RESET
  7. OPTIONAL BINDINGS
  8. MULTI-METHOD LISTENERS

Using the Butter Knife Library


Step1: Configure Your Project for Android Butter Knife

Before getting started with Butter knife, you need to configure your Android project.
  • Update Project-level build.gradle
 buildscript {  
     repositories {  
        mavenCentral()  
     }  
     dependencies {  
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'  
      }  
  }  
  • Apply “android-apt” in module-level build.gradle
 apply plugin: 'android-apt'  
 android {  
 ...  
 }  
 dependencies {  
 compile 'com.jakewharton:butterknife:8.2.1'  
 apt 'com.jakewharton:butterknife-compiler:8.2.1'  
 }  


Finally sync project.

  • Add Butter Knife in Library Module
              Add Butterknife plugin to project-level build.gradle. 

  buildscript {  
     repositories {  
          mavenCentral()  
      }  
   dependencies {  
         classpath 'com.jakewharton:butterknife-gradle-plugin:8.2.1'  
       }  
   }  

     Apply ButterKnife plugin in library module.
 apply plugin: 'com.android.library'  
 apply plugin: 'com.jakewharton.butterknife'  

Note: If you applied ButterKnife in library project then you neeed to use R2 class instead of R class in ButterKnife annotations.


Step2: Use the Annotations

Annotate fields with @BindView and a view ID for Butter Knife to find and automatically cast the corresponding view in your layout.

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

Instead of slow reflection, code is generated to perform the view look-ups. Calling bind 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 bind(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);  
 }  

You can also perform binding on arbitrary objects by supplying your own view root


 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.phot);  

RESOURCE BINDING 

Bind pre-defined resources with @BindBool, @BindColor, @BindDimen, @BindDrawable@BindInt,@BindString, which binds an R.bool ID (or your specified type) to its corresponding field.
 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) field  
  // ...  
 }  

VIEW LIST

You can group multiple views into a List or array.
 @BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })  
 List<EditText> nameViews;  
Then 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.
 ButterKnife.apply(nameViews, View.ALPHA, 0.0f);  

NON-ACTIVITY BINDING

You can also perform binding on arbitrary objects by supplying your own view root.
 public class FancyFragment extends Fragment {  
  @BindView(R.id.button1) Button button1;  
  @BindView(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;  
  }  
 }  
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 {  
   @BindView(R.id.title) TextView name;  
   @BindView(R.id.job_title) TextView jobTitle;  
   public ViewHolder(View view) {  
    ButterKnife.bind(this, view);  
   }  
  }  
 }  

Calls to ButterKnife.bind can be made anywhere you would otherwise put findViewById calls.
Other provided binding APIs:
  • Bind arbitrary objects using an activity as the view root. If you use a pattern like MVC you can bind the controller using its activity with ButterKnife.bind(this, activity).
  • Bind a view's children into fields using ButterKnife.bind(this). If you use <merge> 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.

LISTENER BINDING

Listeners can also be automatically configured onto methods.
 @OnClick(R.id.submit)  
 public void submit(View view) {  
  // TODO submit data to server...  
 }  
All the 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 RESET

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.
 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;  
  }  
  @Override public void onDestroyView() {  
   super.onDestroyView();  
   unbinder.unbind();  
  }  
 }  

OPTIONAL BINDINGS
By 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 fields or the @Optional annotation to methods.
Note: Any annotation named @Nullable can be used for fields. It is encouraged to use the @Nullable annotation from Android's "support-annotations" library.
 @Nullable @BindView(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);  

Add a static import for ButterKnife.findById and enjoy even more fun.

Reference

No comments:

Post a Comment