Implemented Technique: Butter Knife

This and next topics explain the techniques used in the implementation of the Kii Balance for Android. This topic explains Butter Knife and the next topic explains fragments.

You can skip these topics if you do not need to get information further as these techniques are out of the scope of Kii Cloud. For more information, search general technical information on the Internet.

Using Butter Knife

Kii Balance uses Butter Knife to simplify the implementation of the user interface.

Butter Knife is used for the following two purposes:

  • Bind user interface parts to fields of classes
  • Bind buttons to methods of listeners

Use the @BindView annotation when you declare fields as below to bind user interface parts to fields of classes. You do not need to call the findViewById() method unlike you would do with a conventional way.

@BindView(R.id.textEdit1)
TextEdit myTextEdit;

Use the @OnClick annotation in a method as below to bind buttons to listeners. You do not need to call the setOnClickListener() method nor prepare listener classes unlike you would do with a conventional way.

@OnClick(R.id.buttonExecute)
void onExecuteClicked() {
}

After the above preparation, execute the ButterKnife.bind() method and the unbind() method in post-processing.

private Unbinder mButterknifeUnbunder;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
  ...
  mButterknifeUnbunder = ButterKnife.bind(this, view);
  ...
}

@Override
public void onDestroyView() {
  super.onDestroyView();
  mButterknifeUnbunder.unbind();
}

What's Next?

Let us review how to use fragments as the second technique used in Kii Balance.

Go to Implemented Technique: Fragments.

If you want to learn more...

  • For more information about Butter Knife, access the website of Butter Knife.
  • If you incorporate Butter Knife into a new project, see the steps in GitHub as a reference instead of the website.