Make Android App Development smoother with Butter Knife - Part 1

Last couple of years I was just exausted with writing lots of boilerplate code (I'm still now exausted!), then Butter Knife comes along to make me happy. A really great library by Jake Wharton.

Butter Knife logo

Here's I'm writing about some sneak peak of project Butter Knife.

Let's look at a simple worthless Android app which contains 4 buttons which does nothing but showing some message for each. Layout file looks like this:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <Button 
            android:id="@+id/button1"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:text="Button 1"/>
    <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 2"/>
    <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 3"/>
    <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 4"/>
</LinearLayout>  

Now if we see the Activity code then it looks like this:

import android.app.Activity;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.Button;

public class MyActivity extends Activity implements View.OnClickListener{  
    Button button1, button2, button3, button4;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        init();
    }

    private void init() {
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(this);
        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(this);
        button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener(this);
        button4 = (Button) findViewById(R.id.button4);
        button4.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:
                deployNuclearBomb();
                break;
            case R.id.button2:
                killAllAliens();
                break;
            case R.id.button3:
                escapeJombies();
                break;
            case R.id.button4:
                beBatman();
                break;
            default:
                break;
        }
    }

    private void deployNuclearBomb() {
        Toast.makeText(this, "deploying nuclear bomb", Toast.LENGTH_LONG).show();
    }

    private void killAllAliens() {
        Toast.makeText(this, "killing aliens", Toast.LENGTH_LONG).show();
    }

    private void escapeJombies() {
        Toast.makeText(this, "escaping from Jombies", Toast.LENGTH_LONG).show();
    }

    private void beBatman() {
        Toast.makeText(this, "collecting money to become rich", Toast.LENGTH_LONG).show();
    }
}

If you're not already bored than probably you're doing great without Butter Knife. But if you are so bored as me then let's look at the code using Butter Knife,

public class MyActivity extends Activity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ButterKnife.inject(this);
    }

    @OnClick(R.id.button1)
    public void deployNuclearBomb() {
        Toast.makeText(this, "deploying nuclear bomb", Toast.LENGTH_LONG).show();
    }

    @OnClick(R.id.button2)
    public void killAllAliens() {
        Toast.makeText(this, "killing aliens", Toast.LENGTH_LONG).show();
    }

    @OnClick(R.id.button3)
    public void escapeJombies() {
        Toast.makeText(this, "escaping from Jombies", Toast.LENGTH_LONG).show();
    }

    @OnClick(R.id.button4)
    public void beBatman() {
        Toast.makeText(this, "collecting money to become rich", Toast.LENGTH_LONG).show();
    }
}

Just like that, you don't have to bind to the button explicitly just to do your work, Butter Knife handles that for you. Butter Knife handles all the wiring stuff and lets you write the codes that matters.

This is just a worthless demo which merely shows the power of Butter Knife. But it's just enough to start to get the taste. Hopefully I'll write an extensive article about it, but till then happy coding.

comments powered by Disqus