view Binding code example
Example 1: view binding in android
buildFeatures {
viewBinding true
}
Example 2: binding fragment android
example:
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
MartianDataBinding binding = DataBindingUtil.inflate(
inflater, R.layout.martian_data, container, false);
View view = binding.getRoot();
binding.setMarsdata(data);
return view;
}
Example 3: using buidfeatures to enable viewbinding
android {
buildFeatures {
viewBinding = true
}
}
Example 4: kotlin binding views to activity
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
id 'kotlin-android-extensions'
}
<Button
android:id="@+id/btn_finish"
(...)
btn_finish.setOnClickListener {
}