Search
Friday 29 March 2024
  • :
  • :

All About Android Butter Knife

Android Butter Knife is an injection and reduction library created by Jake Wharton. Butter Knife is simple, small and lightweight which allows to perform injection. Using Butter Knife user can perform injection on Views, Objects and onClickListeners. This allows developer to concentrate on login instead of writing boring redundant code.

For configuring Butter Knife in Android project you need to change project level and application level build.gradle files.

  1. In project level Build.gradle add below line in dependencies section
    classpath ‘com.neenbedankt.gradle.plugins:android-apt:1.8’
  2. In application level build.gradle
    1. Add below two lines in dependencies section
      compile ‘com.jakewharton:butterknife:8.1.0’
      apt ‘com.jakewharton:butterknife-compiler:8.1.0’
    2. On top of build.gradle add
      apply plugin: ‘android-apt’

Finally sync gradle files and your are done with the required steps for configuring Butter Knife library with your project.

For checking Butter Knife library implementation we will create a simple application with two labels, two input boxes and one button.

Start with creating layout file activity_main.xml, xml will look something like below.

<?xml version="1.0"encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.75"
android:text="Name"/>
<EditText
android:id="@+id/et_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_number"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.75"
android:text="Number"/>
<EditText
android:id="@+id/et_number"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="phone"/>
</LinearLayout>
<Button
android:id="@+id/btn_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Submit"/>
</LinearLayout>

Now that we have required layout, create new class named ButterKnifeDemoActivity.java and extend it with AppCompatActivity and add below code:-

public class ButterKnifeDemoActivity extends AppCompatActivity{
@BindView(R.id.tv_name)
TextView tvName;
@BindView(R.id.et_name)
EditText etName;
@BindView(R.id.tv_number)
TextView tvNumber;
@BindView(R.id.et_number)
EditText etNumber;
@BindView(R.id.btn_subnit)
Button btnSubmit;
@BindString(R.string.tv_lbl_name)
String lblName;
@BindString(R.string.tv_lbl_number)
String lblNumber;
@BindString(R.string.btn_lbl_submit)
String lblSubmit;
@BindString(R.string.err_name)
String errName;
@BindString(R.string.err_number)
String errNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ButterKnife.bind(this);
tvName.setText(lblName);
tvNumber.setText(lblNumber);
btnSubmit.setText(lblSubmit);
}
@OnClick(R.id.btn_subnit)
public void buttonClick(){
if (etName.getText() == null || etName.getText().toString() == null || etName.getText().toString().length() == 0){
Toast.makeText(this, errName, Toast.LENGTH_LONG).show();
return;
}
if (etNumber.getText() == null || etNumber.getText().toString() == null || etNumber.getText().toString().length() == 0){
Toast.makeText(this, errNumber, Toast.LENGTH_LONG).show();
return;
}
Toast.makeText(this, "You have successfully inputted all the values", Toast.LENGTH_LONG).show();
}
}

Let us understand the code.

With Butter Knife @BindView annotation we injected two text views tvName & tvNumber, two edit texts etName & etNumber, button btnSubmit.

With @BindString annotation we injected string resource in variables lblName, lblNumber, lblSubmit, errName, errNumber.

Next we created a method named buttonClick and injected it with @OnClick annotation to handle btnSubmit click.

Finally inside onCreate we called ButterKnife.bind(this) to bind the injected with this activity and then we set the labels to textviews and implemented code for input validation on buttonclick.

Run the application and click on btnSubmit, if you have not entered Name or Number it will  show appropriate error message of Invalid Name or Invalid Number and if you have entered both the inputs a toast with message You have successfully inputted all the values will be displayed.

As you can see using Butter Knife all your redundant code for findview by id are replaced by @BindView, calls for getString(resId) are replaced by @BindString.

Author Bio:
This article has been written by Rosina De Palma who is a techncial writer at Nex Mobility especially for Mobile App development like Android, iOS, Black Berry and Xamarin. She also working as an android developers India at Nex Mobility. She here to come with new android library for development called Android Butter Knife which is created by Jack Wharton.



Vijay is a compulsive blogger who likes to educate like-minded people on various new technologies and trends. He works with Aegis SoftTech as a software developer and has been developing software for years. Stay Connected to him on Facebook and Google+.