Use Butter Knife library to inject views in Android

 
    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.

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:

pic name pic name pic name
Reference:
Jake Wharton Pages:
- Github: https://github.com/JakeWharton
- Website: http://jakewharton.com/
(Sorry for having ads)

Share


Previous post
« Prev Post
Next post
Next Post »