butter knife tutorial android
What is Butter knife :
- Butter-knife is a library it was developed by Jake Wharton at Square.
- Butter-knife is used for remove repetitive lines of code for Binding views (like : TextView txtView = findViewById(R.id.txtView);
How To Import Butter-knife in Android Project :
implementation 'com.jakewharton:butterknife:(insert latest version)'
annotationProcessor 'com.jakewharton:butterknife-compiler:(insert latest version)'
Where We use :
- View Binding
- Event binding
- Resource Binding
View Binding:
- @Bindview is butter-knife annotation it used for bind-views like text-view,button-view,edit-text .
- Butter knife automatically finds view and it convert into specified view.
example :
activit_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/txtView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@BindView(R.id.txtView)
TextView txtView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
txtView.setText(" Hello From Butter Knife ");
}
}
Resource Binding :
- @BindBool, @BindColor, @BindDimen, @BindDrawable, @BindInt, @BindString are annotations from butter-knife used for bind resources.
Sample
public class MainActivity extends AppCompatActivity {
@BindView(R.id.txtView)
TextView txtView;
@BindDrawable(R.mipmap.ic_launcher)
Drawable img;
@BindString(R.string.str_butterknife)
String str_intro;
@BindColor(R.color.colorAccent)
int color;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
txtView.setText(str_intro);
}
}
How to Bind butter-knife in activity and Fragment :
In Activity :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
in fragment :
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fancy_fragment, container, false);
ButterKnife.bind(this, view);
return view;
}
In Adapter
static class ViewHolder {
@BindView(R.id.title) TextView name;
@BindView(R.id.job_title) TextView jobTitle;
public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
how to perform click events using Butter-knife :
public class MainActivity extends AppCompatActivity {
@BindView(R.id.txtView)
TextView txtView;
@BindView(R.id.btn_button)
Button btn_button;
@BindDrawable(R.mipmap.ic_launcher)
Drawable img;
@BindString(R.string.str_butterknife)
String str_intro;
@BindString(R.string.str_hai)
String str_hai;
@BindColor(R.color.colorAccent)
int color;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
txtView.setText(str_intro);
}
@OnClick(R.id.btn_button)
public void sayHello() {
txtView.setText(str_hai);
}
}
What is Optional Binding in Butter-Knife :
When we using Bind-view exception throws if target was not found , to overcome we have@Nullable and @Optional annotations in butter knife
- @Nullable used for fields
@Nullable @BindView(R.id.might_not_be_there) TextView mightNotBeThere;
@Optional @OnClick(R.id.maybe_missing) void onMaybeMissingClicked() {
// TODO ...
}
No comments