Android butterKnife library tutorial with easy explanation.

In this tutorial,we will learn about the android butterknife and its uses.

Introduction of butterknife

Android butterknife is a lightweight library.

It is a view binding tools which uses the annotation processing to generate boilerplate code for us.

It removes the findViewById(R.id.view) and uses the @BindView(R.id.view).

Android  butterknife dependency

To use the butterknife in our project we need to add the below code into the build.gradle file.

implementation 'com.jakewharton:butterknife:10.1.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'

Android butterknife Bind

Before using view in android,first we need to bind butterknife on the onCreate( ) in the case of Activity.

ButterKnife.bind(this)

In the case of fragment,within the onCreateView( ) method.


LayoutInflater inflater;
View view=inflater.inflate(R.layout.fragment,null);
Butterknife.bindt(this,view);

Android butterknife example

In case of Activity
Public class MainActivity extends Activity
{
@BindView(R.id.textview) TextView textview;
@BindView(R.id.button) Button button;

@Override
Public void onCreate(Bundle SavedInstanceState)
{
super.onCreate(SavedInstanceState);
setContentView(R.layout.main_activity);
ButterKnife.bind(this);
textview.setText(“Android butterknife activity example”);

@OnClick(R.id.button)
Void submit()
{
//Here we can submit the information  on the button click.

}
}
}

In case of fragment

In a fragment,butterknife is same as activity.
Only need to change

ButterKnife(this,view) on  the onCreateView( ) method.


For more information about Butterknife library visit:https://github.com/JakeWharton/butterknife

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *