Use view binding instead of findViewById in Android

Tram Ho

Preamble

On Android 3.6, View Binding gives you the ability to replace findViewById with binding objects created to simplify code, eliminate errors, and avoid all findViewById ‘s boilerplate.

TL; DR

  • Enable view binding in build.gradle (regardless of the libraries implemented)
  • View binding creates link objects for every layout in your module ( activity_awesome.xmlActivityAwesomeBinding.java )
  • The binding object contains an attribute for every view with an id in the layout – with each type exactly and not null .
  • Full support for both Java and Kotlin.

Update build.gradle to enable view binding

You do not need to add any additional libraries to enable view binding. It is integrated in the Android Gradle Plugin starting with versions from Android Studio 3.6. To enable view binding, configure viewBinding in build.gradle at your module level.

In Android Studio 4.0, viewBinding has been moved into buildFeatures and you can use:

When enabled in a project, view binding will create a link layer for all of your layouts automatically. You do not need to make changes to your xml files – it will automatically work with your existing layouts.

View binding works with your current XML and will create a binding object for each layout in a module

Using view binding in an Activity

If you have a layout called activity_awesome.xml , containing a Button and two TextView , view binding will create a small class called ActivityAwesomeBinding that contains an attribute for every view that has an ID in the layout.

You don’t need to call findViewById when using view binding – instead just use the provided properties to reference any view in the layout with the view id.

Use view binding safely

findViewById is the source of many bugs that occur in Android. It can link to an id that is not in your reference layout – generating null and crash errors. View binding replaces findViewById a shorter and more secure way. And since binding classes are created as regular Java classes with Kotlin-friendly annotations, you can use view binding on both Java and Kotlin programming languages.

What code does it generate?

View Binding creates a Java class that replaces the need for findViewById in your code. It will create a binding object for every XML layout in your module while mapping the names so activity_awesome.xml maps to ActivityAwesomeBinding.java . When editing the XML layout in Android Studio, the code generation will be optimized to only update the associated object associated with that XML file and it will do so in memory to help things quickly. This means that binding object changes are immediately available in the editor and you don’t need to rebuild the project.

Let’s see what view binding has generated through the small example below:

activity_awesome.xml

And here is the view binding generated code from the above XML file:

View binding will create an attribute that is entered correctly for each view with a specified id. It will also create an attribute called rootView that can be used via a function called getRoot . View binding does not implement any logic. It only displays your view in a linked object so you can connect them without error-prone calls to findViewById . This keeps the file created simply (and avoids slowing down the build project).

If you use Kotlin, this class is optimized for interoperability. Because all properties are annotated with @Nullable or @NonNull , Kotlin knows how to display them as unsafe. To learn more about the intersection of languages, see the documentation for Java calls from Kotlin .

In ActivityAwesomeBinding.java , view binding creates a public inflate method. Version of an argument converts null into a parent view and is not attached to the parent. View binding also displays an inflate version of the three arguments that allow you to pass parent and attachToParent as needed. Call bind is where the magic happens. It will inflate the layout and link all attributes, with some error checking added to create readable error messages.

Use View binding or ButterKnife

One of the most common questions asked about view binding is “Should I use Kotlin’s view binding or ButterKnife?”. Both of these libraries have been successfully used by many applications and solved the same problem. For most applications, we recommend that you try view binding instead of these libraries because view binding provides a shorter, more secure view lookup.

Refer

Use view binding to replace findViewById

Share the news now

Source : Viblo