Use view binding instead of findViewById

Tram Ho

1. Introduction

View binding was introduced from Android Studio 3.6 . View binding is a new feature that allows you to replace the boring findViewById command with automatically generating binding classes for each XML layout file to simplify code, eliminate bugs, and avoid boilerplate compared to using findViewById. .

2. Use

Note: View binding is available in Android Studio 3.6 version

To use View binding in the module, add viewBinding to the build.gradle file:

If you want to skip a file without gen view binding automatically, add the following attribute to each xml file:

Once viewbinding is enabled, class binding will automatically generated for each XML layout file. The name of the class binding was generated by converting the XML layout file name into a camel case and adding the final Binding suffix.

For example, file xml activityawesome.xml -> ActivityAwesomeBinding

2.1. Using viewbinding in Activity:

To set the instance of the binding class used in the activity, in the onCreate () method, follow these steps:

  1. Using the inflate () method, an instance of class binding will be created for use in the activity.
  2. Call to root view reference with getRoot ()
  3. Pass root view into setContentView () to display the view

You can now use the instance of the binding class to reference the elements of the view

2.2. Using view binding in Fragment:

To use instance binding class in fragment, in onCreateView () method:

  1. Using the inflate () method, an instance of class binding will be created for use in fragment.
  2. Call to root view reference with getRoot ()
  3. Return root view from the onCreateView () method to display the view

You can now use the reference variables of the view:

2.3. How is the code generated:

View binding automatically generates 1 class binding for each xml file. When you edit the layout xml file, the automatic gene code will be optimized only for class binding related to that xml file and it will be implemented in memory to ensure everything is as fast as possible. Let’s take a look at an automatic gene file from the xml layout file:

View binding will genetically match the type for each view identified by the id assigned to that view.

Layout include:

View binding is also used in include file layout xml

Note: tag <include> have id android: id = “@ + id / includes”

View binding will automatically reference IncludedButtonsBinding in ActivityAwesomeBinding

3. View binding vs findViewById

View binding has a few points better than findViewById:

  • Null safety : Because view binding creates a direct reference to the view, there is no risk like Null Pointer Exception when invalid view ID.
  • Type safety : Fileds in each class binding have type matching views with references in the xml file. Therefore, there is no risk with the cast class exception.

4. View binding vs Data Binding

View binding and Data binding both generate automatic class binding for you to use the direct view reference. However, view binding is more noticeable in simple use cases and offers a number of features:

  • Faster compile: View binding requires no annotation, so compile time is faster
  • Easy to use: View binding doesn’t require a tag for the xml file

Besides, view binding also has some disadvantages compared to data binding:

Note: View binding only replaces findViewById, if you want to automatically bind view in layout xml file, you can use view binding with data binding in the same module.

5. View binding vs Kotlin synthetics vs ButterKnife

Above are 3 ways to use view in xml layout file and be used by everyone.

ButterKnife validate nullable / not-null at runtime, the compiler does not check whether you match the layout correctly or not

You should consider using view binding!

Refer:

https://developer.android.com/topic/libraries/view-binding

https://medium.com/androiddevelopers/use-view-binding-to-replace-findviewbyid-c83942471fc

Share the news now

Source : Viblo