What is butterknife example stackoverflow?
Answers
Before getting started with Butter Knife, you need to configure your Android project.
Open build.gradle at project level and add the following dependency
dependencies { ... classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' }Open build.gradle(Module:app) (module level) and add the following dependencies.
dependencies { ... compile 'com.jakewharton:butterknife:8.1.0' apt 'com.jakewharton:butterknife-compiler:8.1.0' }And apply the plugin:
apply plugin: 'android-apt' android { ... }Finally sync Gradle.
Butter Knife in actionNow that you have configured Butter Knife its time to move on to the main features this injection library can provide:
Binding views and resourcesEvent ListenersList adapters, RecyclerView implementationWhy use Butter KnifeThe goal of this library is to help developers write better code, and it does so by trying to reduce the code used in the onCreate method on an Activity class and in onCreateView on Fragments. All you need to write in these methods is the following:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //... ButterKnife.bind(this); //... } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_blank, container, false); ButterKnife.bind(this, view); return view; }In both the cases Butter Knife binds to the specified Android Activity or View targets to correctly act on the injections specified in these classes. All the view finding lines of code and the action listeners are implemented elsewhere, leaving these methods as clean as possible.