Step 1 - Enable View Binding in build.gradle file
buildFeatures{
viewBinding true
}
Step 2 - in your activity.java file initialize binding with your activity here MainActivity.java
before onCreate Method
ActivityMainBinding binding;
after onCreate Method
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
after this binding.id_name.setOnClickListener
Binding in Fragment -
in your fragment.java file initialize binding with your activity here BlankFragment.java
FragmentBlankBinding binding;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
binding = FragmentBlankBinding.inflate(inflater, container, false);
//here write your code
return binding.getRoot();
}
Tags:
Android Development