Code injection is usually something we try and avoid, but Jake Wharton is using injection for good. With his open source “Butter Knife” library, you can use annotations to conveniently create commonly used code for Views in Android. In this post, I present how to implement it in an Android Project.
This post is part of a series called Android TOP useful libraries
1. Setup in project
- Download jar file at it's HOME PAGE.- Add jar to your Android project(also java build) and configure appropriate annotation settings. See here for instructions for Eclipse and here for instruction for IntelliJ.
2. Implement
- Create an activity layout and a fragment layout simple like these:- In programmatically code, we must:
+ Declaring "inject" views from xml.
+ Set events for them (simply is using them).
Full java code:
- ButterKnifeActivity.java:
- ButterKnifeFragment.java:
Okey, as you can see, codes is simple and after build and run program, we have this result:
Jake Wharton Pages:
- Github: https://github.com/JakeWharton
- Website: http://jakewharton.com/
(Sorry for having ads)