Skip to main content

Easy View Injection with Butter Knife






    Butter Knife is an easy to use View Injection library. It significantly cleans up some boiler-plate code, specifically dealing with setting up Views. For whatever reason I haven't used it until recently but it is easy to adapt to and greatly reduces the amount of code I have to write. As an aside I would like to point out that Square really does a great job with their libraries (Butter Knife is a library by Jake Wharton a developer at Square). Not only are they coded really well but they're also open source and they have so many significant ones (Picasso, OkHttp, Retrofit, Dagger, etc).

Note: In some of my code snippets I use private variables for the Views but when using Butter Knife, the Views must be public. 

    Butter Knife is so easy to use you really don't need a tutorial since they cover most use cases on its Github page. But either way we're going to have a look at how to use the library and compare it to how you would normally code without it. First, let's have a look at the way we would normally instantiate a View from a layout resource file:


TextView title = (TextView) findViewById(R.id.title);


    We use the Activity class' findViewById() method, passing it an integer ID as a parameter, to get an instance of a View but we have to cast it to the specific View it is (TextView). Now, let's take a look at how we would do this using Butter knife:


@Bind(R.id.title) TextView title;
ButterKnife.bind(this);


    It might not be a significant decrease in the amount of code we have to type (especially when considering the ButterKnife.bind() call) but in the long run it is much better. Also, programmers tend to declare their View variables in the beginning of the class (making them instance variables) and then instantiate them later in the onCreate() method. This causes the amount of lines used to double and makes it harder to read. Which brings up another good reason to use Butter Knife: it keeps everything in one place so no need to go looking for the instantiation of a variable. So, let's look at a more realistic example, first how we normally would code it:


public class MainActivity extends AppCompatActivity{
    private TextView title;
    private TextView date;
    private TextView content;
    private ImageView image;
    private Button like;
    private Button dislike;
    private Button comment;
    
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        title = (TextView) findViewById(R.id.title);
        date = (TextView) findViewById(R.id.date);
        content = (TextView) findViewById(R.id.content);
        image = (ImageView) findViewById(R.id.image);
        like = (Button) findViewById(R.id.like);
        dislike = (Button) findViewById(R.id.dislike);
        comment = (Button) findViewById(R.id.comment);
    }

}


    And this is what it would like if we used Butter Knife:


public class MainActivity extends AppCompatActivity{
    @Bind(R.id.title) private TextView title;
    @Bind(R.id.date) private TextView date;
    @Bind(R.id.content) private TextView content;
    @Bind(R.id.image) private ImageView image;
    @Bind(R.id.like) private Button like;
    @Bind(R.id.dislike) private Button dislike;
    @Bind(R.id.comment) private Button comment;

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

}


    That's much better. If you programmed a decent amount you would jump at any chance to cut down on redundant code and the need to type it out. Butter Knife has more features, such as, binding text to TextView's and images to ImageView's but we won't look at them here.

Comments

Popular posts from this blog

Face detection and live filters

Live video filters are becoming a popular trend fueled by Facebook (through their purchase of Msqrd) and Snapchat incorporating the features into their apps. These filters apply images or animations to your face using face tracking software. This technology has been around for awhile but is becoming increasingly more common due to the powerful CPU's that our mobile phones now have. Google provides an API that provides face tracking abilities through the Google Play Services library called Mobile Vision. I'm going to use their API to build a basic live filter app. The end result will look something like this:


    The bounding box wraps around the detected face and the sunglasses are the filter I chose (which is just a PNG image) which are drawn over the eyes. You could use any PNG image (with alpha for the background) you want, you will just have to adjust the layout according to where the image should be displayed. As you move your head, the box and sunglasses are redrawn…

Setting Up Connection Pooling With Elastic Beanstalk

Amazon's Elastic Beanstalk is a service which automatically scales your application when needed. It uses Amazon's Elastic Compute Cloud (EC2) instances as deployable containers which when your app requires more resources more containers will be deployed. This removes the need to manually configure your EC2 instance whenever you need more connections or resources and attempts to add simplicity to the maintenance aspect of your application. So, when you get more users of your app, your app will scale accordingly.

    Unfortunately, along with the ability to scale automatically, comes less control and configuration. Things you would normally have the ability to configure to your liking, such as your server, you no longer can. Amazon attempts to address this issue with configuration files. You can provide configuration files which can set up your server. These files are either written in JSON or the horrible format YAML. Though these files provide some level of control, you ca…

Android Guitar Tuner

Recently I created a guitar tuner application for Android that is written with pure Java (no C++ or NDK usage). The design was inspired by the Google Chrome team's guitar tuner web app using the WebAudio API. I wanted to code a version written natively for Android that didn't have to rely on a WebView, the WebAudio APIs, or server-side logic. Also, I wanted this application to be available to as many versions of Android as possible (whereas the WebAudio API may only be supported in more recent versions of WebView available only on newer flavors of Android). So, I coded it from scratch. I used a portion of the open source TarsosDSP project (their YIN algorithm) to help with the pitch detection.

    The application is available in the Google Play Store for Android: https://play.google.com/store/apps/details?id=com.chrynan.guitartuner. The project is completely open source and the code can be found on the GitHub repository: https://github.com/chRyNaN/Android-Guitar-Tuner. Fi…