View Binding in Android: Activities, Fragments, Dialogs, and RecyclerView Adapters
Whenever we start a project the most monotonous work is to initialize each of the views from our layout files and using findViewById again and again. For a long time, we have been doing this but now ViewBinding can save your time.
What is View Binding? According to the official 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 short its a replacement for our old friend findViewById.
Why use View Binding:
- Null safety
- Type safety
- Faster compilation
- Ease of use
To use View Binding in your projects your Android Studio version should be at least 3.6.
In your build.gradle file add the following code:
android{
...
viewBinding {
enabled = true
}
}
From Android Studio version 4.0 use the following code:
android{
...
buildFeatures {
viewBinding = true
}
}
Let's see how itis implemented.
I am adding all the XML files here and you can see that I haven't done anything special here everything is what we do normally.
We usually use layout files in
- Activities
- Fragments
- Dialogs
- Recycler View Adapters
Let's take each one of them one by one.
In Activities:
To use view binding in activities add the following code instead of setContentView(R.layout.some_layout)
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
binding.greetingText.setText("Hello, Reader.");
}
}
Now you can access any view added in your activity_main.xml file. For example to get a reference of the TextView use binding.greetingText and all the methods available for a TextView will be available to you.
In Fragments:
In the onCreateView() method use the following code:
private FragementLayoutBinding binding;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = FagementLayoutBinding.inflate(inflater,container,false);
View view = binding.getRoot();
return view;
}
In DialogFragments:
In onCreateDialog() method set the custom views by the following code:
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
binding = DialogLayoutBinding
.inflate(LayoutInflater.from(getContext()));AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setView(binding.getRoot());
binding.dialogTextView.setText("I am Dialog's TextView");
return builder.create();
}
In RecyclerViewAdapters:
In onCreateViewHolder method add the view like this:
public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(mContext);
binding = ListItemBinding.inflate(inflater,parent,false);
return new CustomViewHolder(binding);
}
Also in your custom class extending ViewHolder class add the following code:
public class CustomViewHolder extends RecyclerView.ViewHolder{
private ListItemBinding itemBinding;
public CustomViewHolder(ListItemBinding itemBinding) {
super(itemBinding.getRoot());
this.itemBinding = itemBinding;
}
}
Here’s the MainActivity
Now run the app and you will something like this:-
Also if you want a layout file to be ignored while generating binding classes, add the tools:viewBindingIgnore="true"
attribute to the root view of that layout file.
You can check out this project from here