SoFunction
Updated on 2025-03-01

Android Studio 3.6 new features of ViewBinding User Guide

View Binding is a feature that makes it easier for you to write code that interacts with views. Once view binding is enabled in the module, it will generate a binding class for each XML layout file present in the module. An instance of a bound class contains a direct reference to all views that have an ID in the corresponding layout.

text

The ViewBinding function has been launched in Android Studio 3.6 Canary 11 and later. ViewBinding will gradually replace findViewById. What are you waiting for? Take the time to learn!
The ViewBinding Demo in Google's official document is written in Kotlin language. It looks rather unfamiliar and took some time. I implemented similar code in Java, but it is more friendly to look at Java!

Here is a brief introduction to how to use ViewBinding:

Revise

Add the viewBinding element to its file, and you need to resync after adding it

// Android Studio 3.6.0
android {
 ...
 viewBinding {
  enabled = true
 }
 }

In Android Studio 4.0, viewBinding will be turned into properties and integrates into the buildFeatures option. The configuration needs to be changed to:

// Android Studio 4.0
android {
 buildFeatures {
 viewBinding = true
 }
}

Modify java code

If your layout file is activity_main.xml, an ActivityMainBinding class will be generated. If your layout file is result_profile.xml, a ResultProfileBinding class will be generated, and so on.
Here is an example of activity_main.xml and its corresponding one:

Suppose activity_main.xml places three controls: TextView (Id is text), Button (Id is button), and ImageView (no Id is set). Because the ImageView does not have an Id set, there is no reference to it in the binding class, so it cannot be referenced in the code.

Some of the codes are as follows:

@Override
 protected void onCreate(Bundle savedInstanceState) {
 (savedInstanceState);
// setContentView(.activity_main);
 LayoutInflater layoutInflater = (this);
 ActivityMainBinding binding = (layoutInflater);
 setContentView(());

 ("The text has changed");
 (new () {
  @Override
  public void onClick(View v) {
  (getApplicationContext(), "Button is clicked", Toast.LENGTH_SHORT).show();
  }
 });
 }

Things to note

Note: The original setContentView(.activity_main) needs to be commented out, otherwise the ContentView will be set repeatedly.
The root view of the layout (activity_main.xml) will automatically generate a member variable named rootView. In the onCreate() method of Activity, you need to pass the rootView into the setContentView() method, so that the Activity can use the layout in the bound object. The rootView is a private variable and needs to be obtained using the getRoot() method.

Engineering code

The complete project is welcome to download on QIYU or Github (ViewBindingTest directory):
/lwjobs/AndroidStudy
/lwjobs/AndroidStudy

For detailed descriptions of view binding ViewBinding, please refer to:

Use view binding to replace findViewById

Summarize

This is the article about the new Android Studio 3.6 feature view binding ViewBinding Guide. For more related contents of Android Studio 3.6 view binding ViewBinding, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!