Tidy Coding using Android ButterKnife

Sometimes Developers get Frustrated and Don't want to rewrite more code stuff. Do you want to make your code Tidy? Do you want to write lesser code?
So here we are...
This Articale will help you to make your code slightly beautiful. Here i will discuss injection library named as Android Butter Knife. This is an open source view 'injection' library created by Jake wharton (Square Inc.).

In our android application code Many times we need to write findViewById(), onClick(), onLongClick(), onItemSelected() again and again for every view that makes our code look and feel bad. 


In the sample project of this tutorial, you can see a sample application with one activity and one fragment with an implementation using the Butter Knife library and a regular implementation. Let's explore the steps involved to integrate the Butter Knife library.





                                        Butter Knife

The source code for the library and sample application are available on GitHub.

Using the ButterKnife library

Step 1: Add the Dependency

Add the following dependency to the project's build.gradle file:


1
compile 'com.jakewharton:butterknife:6.1.0'


Find the steps in the screenshot.

Step 2: Getting Started With ButterKnife

Which would you like to use from the below codes:

The standard code which we write nowadays find Views from my_activity.xml


1
2
3
4
5
6
7
    Button button = (Button)findViewById(R.id.button);
    TextView textView = (TextView)findViewById(R.id.textView);
    RadioButton radioButton  = (RadioButton)findViewById(R.id.radioButton);
    CheckBox checkBox= (CheckBox)findViewById(R.id.checkbox);
    EditText editText = (EditText)findViewById(R.id.editText);
    VideoView videoView = (VideoView)findViewById(R.id.videoView);
    WebView webView = (WebView)findViewById(R.id.webView);

 Android butterknife code


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    @Bind(R.id.button)
    Button button;
    @Bind(R.id.textView)
    TextView textView;
    @Bind(R.id.radioButton)
    RadioButton radioButton;
    @Bind(R.id.checkBox)
    CheckBox checkBox;
    @Bind(R.id.editText)
    EditText editText;
    @Bind(R.id.videoView)
    VideoView videoView;
    @Bind(R.id.webView)
    WebView webView;


I personally prefer the second block, it’s smaller and well structured. This is how Android Butter Knife improves views in the layout file. 


Using butterknife for something Usefull

The simplest piece code you are likely to use while coding for Android is a Toast Message:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
@Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

    Button button = (Button)findViewById(R.id.button);

         button.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 Toast.makeText(getApplicationContext(),"Hello Android",Toast.LENGTH_LONG).show();

             }
         });

     }

Here's the butter knife code for the same:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
@Bind(R.id.button)
    Button button;

    @OnClick(R.id.button)
    protected void Toast(){
        Toast.makeText(getApplicationContext(),"Hello Android Butter Knife",Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

    }

It is clean, simple and easy to understand. In the onCreate() method, insert ButterKnife.bind(this) and the anonymous inner class inside the listener is not needed.

Conclusion

Android Butter Knife is a useful library to develop more beautifully coded Android Apps. It’s easy to learn even for beginners, so be sure to try it out.
Tidy Coding using Android ButterKnife Tidy Coding using Android ButterKnife Reviewed by Unknown on 00:26 Rating: 5

No comments:

Powered by Blogger.