Skill Level Intermediate
- [Instructor] Android developers are always looking for a way to get their work done with less code. Getting references to view object and setting up event handlers takes a good bit of boilerplate code and you very quickly start wondering if there's a better way. There's a popular open source library named ButterKnife. It's available at this web page. The purpose of ButterKnife is to streamline your code, moving away from the use of the findViewById method and toward the use of annotations.
To get started with ButterKnife, go to this page and scroll all the way down to the bottom. Under the Download section, under Gradle, select and copy these two declarations. Then go back to Android Studio and open your Gradle build file for your app module. Paste those declarations at the bottom and then re-sync Gradle. ButterKnife is now available to your application.
In the main activity class of this application there are two view references, one for a TextView and the other for a CoordinatorLayout. In the onCreate method, there are two equivalent assignments using findViewById, one for the TextView again and one for the CoordinatorLayout. Now this isn't a lot of code, but the code is scattered. You have the references and the assignments in two different places. Using ButterKnife, you can bring them together.
I move the cursor up here, to my fields and above the declaration of the TextView, I'll use an annotation named BindView. This is a part of the ButterKnife library. When you call the BindView annotation, you pass in the resource id of the View object you want to bind to. That's the same resource id that's being used here. So I'll copy it and I'll paste it here. And now I'm saying, use this field and assign it to this View object.
When you assign a View reference in this way it cannot be private and it also can't be static. That's because ButterKnife works by generating a bunch of code in another class, and it has to be able to reference this field from outside the activity class. So I'll just get rid of the private keyword and I'll do that for CoordinatorLayout too and then I'll add another call to BindView and I'll reference R.id.coordinator_layout. And now I can come down to my onCreate method and comment out the two calls to findViewById.
There's one more critical step, you must explicitly bind these references by a call to a static method of the ButterKnife class. And you have to do that after you load your Layout file. So I'll move the cursor right here to after setContentView and I'll call ButterKnife.bind. And notice you can pass in an activity, a dialogue or a number of other kinds of visual objects. I'll just pass in this, the current activity and again this line of code must be after you've loaded your Layout file.
And now, I'll run my application. When I click the Run Code button everything still works as expected. I'm displaying messages successfully in the TextView and I'm also successfully displaying a Snackbar message which is using that CoordinatorLayout allowing the user to swipe to dismiss it. Now you can also use ButterKnife to declare event handlers. In this little application, my event handlers are being declared in my Layout file and once again, that means my code is scattered round in a number of different places.
If I were to come back to this application a few months later, it might take me some time to figure out that I was declaring my onClick events in my Layout file. And more importantly, it's not just in the first Layout file it's in the nested one, Content made. These two buttons have onClick event handlers set as attributes. I'm going to get rid of those, I'll just delete them from both the run button and the clear button. And then I'll make sure that I have Id's assigned to these two buttons.
Then I'll come back to main activity and this time I'll use an onClick annotation and I'll pass an R.id.run_button and then I'll go down here and take this runCode method and then I'll select and copy it to the clipboard, I'll come back up here and paste it and I'll get rid of the View argument.
And I'll come back down here and I'll comment out this version of the runCode method. I don't need that one anymore. Notice that runCode is dimmed out, it says it's never used. But then I'll select Build, Make Project. And after a moment, I'll see that that method is recognized and that's because a bunch of code has been generated in the background that's now calling that method. I'll do the same thing for the clear button. I'll add an onClick annotation and pass an R.id.clear_button, I'll come down here and this time I"ll just cut this to the clipboard, and I'll get rid of this comment, that's not needed anymore.
And I'll move up here and paste that code in. Once again, I'll get rid of the View argument, if you want to pass it in, you can, but it's no longer required. And then once again, I'll make my project. And I'll see that the clearLog method is now recognized. And I'll run the application, and I'll test my buttons and my runCode method works as expected and my Run Code and my Clear Log buttons work as expected. I'll click Run Code a number of times and then I'll click Clear Log.
There's a lot more to ButterKnife than what I've shown here. For more information about this incredibly valuable library, go back to the website at http://jakewharton.github.io/butterknife/ you'll find a lot more documentation and a lot of great examples of how you can use this library.