How to Implement ViewBinding in Android Activity, Fragments And RecyclerView - Java
Hello Learners,
Before we start, go through the official documentation of Android Developers
https://developer.android.com/topic/libraries/view-binding
Now, According to the documentation,
View binding is a feature that allows you to more easily write code that interacts with views. Once view binding is enabled in a module, it generates a binding class for each XML layout file present in that module. An instance of a binding class contains direct references to all views that have an ID in the corresponding layout.
In most cases, view binding replaces findViewById
.
I just copy-pasted the definition In-case you haven’t gone through the documentation link.
So let’s start implementing
Enabling ViewBinding in your project requires this code:
android {
...
buildFeatures {
viewBinding true
}
}
in the module-level build.gradle
file.
Now, In case if you don't want any XML file to have a viewBinding class to be generated so do this thing in your root layout.
<LinearLayout
...
tools:viewBindingIgnore="true" >
...
</LinearLayout>
If view binding is enabled for a module, a binding class is generated for each XML layout file that the module contains. Each binding class contains references to the root view and all views that have an ID. The name of the binding class is generated by converting the name of the XML file to the Pascal case and adding the word “Binding” to the end.
For Example, activity_welcome.xml gets a Binding class named ActivityWelcomeBinding.
Now let’s come to our main topic; implementation of viewBinding.
In Activities
https://developer.android.com/topic/libraries/view-binding#activities
Go through this link to know what documentation says.
And the example of implementing viewBinding is here:
Note: Any Binding class has a reference to only those views which have IDs so don’t forget to assign ID’s to views in your XML file
The Instance we going to use later on
ActivityWelcomeBinding activityWelcomeBinding;
We are going to set up this instance in the OnCreate method
activityWelcomeBinding = ActivityWelcomeBinding.inflate(getLayoutInflater());
here we called the inflate method generated by these classes to which we can inflate the layout class and use it in the activity.
And Now,
setContentView(activityWelcomeBinding.getRoot());
we added the root view of the Binding class as the ContentView for the Activity.
And from this instance, we can call the fields and work accordingly as per our needs.
the code:
public class Welcome extends AppCompatActivity {
ActivityWelcomeBinding activityWelcomeBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityWelcomeBinding = ActivityWelcomeBinding.inflate(getLayoutInflater());
setContentView(activityWelcomeBinding.getRoot());
}
}
In Fragments
https://developer.android.com/topic/libraries/view-binding#fragments
Go through this link to know what documentation says on how to implement viewBinding in Fragments
The inflate()
method requires you to pass in a layout inflater. If the layout has already been inflated, you can instead call the binding class's static bind()
method.
The Example of ViewBinding in Fragment is here:
It is similar to implementing in Activities but here we use the arguments of the OnCreateView method like this…
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mainFragmentBinding = MainFragmentBinding.inflate(inflater, container, false);
Tip: press Ctrl+P to look out the parameters of a method
Now we return the root view for the OnCreateView method
return mainFragmentBinding.getRoot();
And here we need to destroy the instance as the fragment’s view has been destroyed.
@Override
public void onDestroyView() {
super.onDestroyView();
mainFragmentBinding= null;
}public class MainFragment extends Fragment {
MainFragmentBinding mainFragmentBinding;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mainFragmentBinding = MainFragmentBinding.inflate(inflater, container, false);
mainFragmentBinding.rvMovies.setLayoutManager(new GridLayoutManager(getActivity(), 2));
mainFragmentBinding.rvMovies.setAdapter(new RecyclerAdapter(getActivity()));
return mainFragmentBinding.getRoot();
}
}
@Override
public void onDestroyView() {
super.onDestroyView();
mainFragmentBinding= null;
}
In RecyclerView Adapter
Now in Recycler view, I made a custom view and its binding class is used in this adapter
In the Inner Class where we extend RecyclerView.ViewHolder
We set up the binding class
public static class Holder extends RecyclerView.ViewHolder {
CustomViewBinding customViewBinding;
public Holder(CustomViewBinding customViewBinding) {
super(customViewBinding.getRoot());
this.customViewBinding = customViewBinding;
}
}
We create the constructor of the class to get the context whenever we call this class’s object.
Context context;
public RecyclerAdapter(Context context) {
this.context = context;
}
Now we can use this context to inflate the view in onCreateViewHolder method
@NonNull
@Override
public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new Holder(CustomViewBinding.inflate(LayoutInflater.from(context), parent, false));
}
And on the onBindViewHolder method, we can interact with the views of custom View
@Override
public void onBindViewHolder(@NonNull Holder holder, int position) {
holder.customViewBinding.ivMovieImage.setImageResource(R.drawable.ic_launcher_background);
}
The whole code:
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.Holder> {
Context context;
public RecyclerAdapter(Context context) {
this.context = context;
}
@NonNull
@Override
public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new Holder(CustomViewBinding.inflate(LayoutInflater.from(context), parent, false));
}
@Override
public void onBindViewHolder(@NonNull Holder holder, int position) {
holder.customViewBinding.ivMovieImage.setImageResource(R.drawable.ic_launcher_background);
}
@Override
public int getItemCount() {
return 4;
}
public static class Holder extends RecyclerView.ViewHolder {
CustomViewBinding customViewBinding;
public Holder(CustomViewBinding customViewBinding) {
super(customViewBinding.getRoot());
this.customViewBinding = customViewBinding;
}
}
}
So this is how we implement ViewBinding in Activities, Fragments, and RecyclerView Adapter class.
If you still feel some confusion, Go through this youtube tutorial
The source code is here: