An easier and simple way to bind UI components in the android screens to the code is explained with the help of butterknife android i.e., butterknife tutorial.

Butter Knife is one of the type of view injection’s available in the coding terminology which uses annotation processing. Let’s get started to our Android Tutorial on ButterKnife || ButterKnife Tutorial || Data Binding

 

Generally its a good practice to use this kind of data binding which make you coding much flexible, easier and even faster compared to the older methodologies.Not only Butter Knife we have other data binding’s which we will discuss in coming tutorials.

 

Not only UI components but we can bind several other category’s like

1) Binding View’s ( @BindView ) –> TextView, Button, Edittext, Checkbox.. and other UI Components

2) Binding String’s (@BindString ) –> Bind Strings from strings.xml file

3) Binding Drawables ( @BindDrawable ) –> Bind drawable images from drawable folder.

4) Binding  Color’s( @BindColor ) –> Bind color from color.xml

5) Binding Dimen’s( @BindDimen ) –> Bind dimension from dimen.xml file

 

Add Butter Knife library to your android studio project

I am using 8.6.0 version check latest version before you add android studio butterknife plugin

 

 

The traditional way of binding views to code is as

for example textview :

( or )

 

which is initialized as following way using butter knife bindview

 

 

Here we are binding the view using @BindView as discussed above it’s a view component so we are using BindView.

 

Also if we include any layout in the view we can initialize them as the above way compared to traditional style where we used to add view before like

 

 

which is initialized as same way we initialize a local view using butter knife

 

But the most important crucial step in Butter Knife is to initialize it before using it in activity, fragment where ever you use it as

In activity’s we pass the activity parameter as “this”

 

In Fragment’s we pass Object “this”  and  Source “view”

 

Bind Different views as

Activity.Java

 

In onCreate() method initialize Butter Knife “butter injection” first  for reference i am specifying imports.

 

 

then

 

 

 

 

then set their values android onclick

 

 

for button click

 

 

Fragment.java

In OnCreate() initialize the Butter Knife which is a slightly different from Activity we pass an extra parameter – android fragment example

 

 

The initialization of views is same as activity

 

 

then you can use it as

 

Now i would like to show usage of Butter knife in a BaseAdapter file

 

In ViewHolder class we initialize views so here is the way we don using Butter Knife

 

 

And as you expect the usage is same as where we use holder object to set values to it

 

 

SampleAdapter.java

butter knife holder

 

 

 

AndroidManifest.xml

No special permissions are required for using Butter Knife.

 

Do share, subscribe, comment and like our tutorials to get more tutorials get connected.

Show Buttons
Hide Buttons
Read previous post:
Android Jobscheduler Tutorial | Job Scheduler

Also next come up the MyJobExecutor     MyJobExecutor.java Adding a separate thread to run in background.   [crayon-5c69dc707ec91789182215/]  ...

Close