View Binding A good alternative to FindViewByIds on Android systems
10 minute(s) read
Published on: Aug 12, 2021
Updated on: Dec 14, 2021
In Android systems, to be able to create and design an application, we need to create some views. FindViewByIds used to be used to create views, but today View Binding has replaced them. In this tutorial, we want to talk about View Binding, which has replaced findViewById. Using findViewById to define views would increase the size of the code and take a lot of time from the programmer and designer, which is why View Binding has replaced it.
What will you learn in this tutorial?
What you will learn in this article are as follows:
- What is View Binding?
- How to build a View Binding project
What is View Binding?
This is one of the features and subsets of Jetpack, which is intended for Android Studio 3.6 version of the Android operating system, and using this feature is possible only in this version of the operating system.
The main reason that Views Binding is used is that when using this feature, there is no need to add a library between the codes, which also reduces the size of the code.
If we have multiple views within the XML layer, we must define each of those views separately in Activity or Fragment using findViewById. Like the following:
If we use View Binding, the programmer no longer needs to define the views, and the views are called automatically by id.
Compare View Binding with other methods in Android systems
- Additional code:
It should be noted that using other methods in Android systems, we need to design a separate variable for each view, which increases the code.
- Lack of Type safety:
This means that errors can occur when defining and creating views and are not completely secure. For example, a view might be a TextView view, and the programmer might mistakenly define it as EditText.
- Not checking Null safe:
If one of the views is empty and we do not fill them in, we may encounter a NullPointerException error because it cannot detect the status of being Null.
This library was one of the most widely used libraries, which in addition to being used to connect views, but also for resources such as strings, colors, and sizes.
Reduce code size:If we use this library, it will reduce the code. Compared to findViewById, this library can reduce the size of the code to some extent. In using this library to define views, we can also use annotations.
After adding this library to the project, we can define each view with the name @BindView using annotation. As follows:
Lack of Type security: This library, like findViewById, is not type security and may have encountered an Exception error during execution.
Reduce build speed: Using this library reduces build speed.
It should be noted that the only major difference between Data Binding and the previous two methods mentioned above is that in this method, a class is automatically generated when activated for each of the layouts or resources.
- Reduce code size:
Using this method can reduce code size. It should be noted that this method reduces the volume of code more than the previous two methods.
- Reduce build speed:
In this method because Annotations are used to define resources, the build speed is also reduced.
- Type safety and Null safe:
This method, unlike the two methods mentioned above, supports these capabilities, and has these capabilities and no worries.
View Binding is a subset of Data Binding that removed findViewById. In other words, it has replaced findViewById and can be used instead.
- Reduce the volume of codes:
If we use this method, the volume of written code will be reduced to a minimum and will be reduced to a greater extent. In this way, if we write the id of each view, we can easily access it.
- Increase build speed:
In this regard, View Binding is the same as Data Binding, and if we enable it and use it, a class is created for each layout, and all views are automatically created within the same class. But the only difference is that annotations are not used for processing, and this increases the build speed.
- Type safety and Null safe:
This method supports these features and has these features and no worries.
How to build a View Binding project
1- First, we activate the View Binding feature in Android Studio.
Of course, it should be noted that versions 3.6 and above can support ViewBinding.
2- To activate it, we must add the following code in the build.gradle (project) file and in the android block.
3- In Android Studio, we create a new project and choose its name ViewBinding. 4- Select the type of activity for this project from the type of Empty Activity. 5- The language used in this project is Java.
6- By activating View Binding, a class is specified for each of the layouts and the name of this class is taken from the name of the related layout. The Binding extension is added to the end of all class names and the type of class name is written in camel case.
7- For example, in this project we have a layout called activity_main.xml, the name of the class that will be created for this layout will be ActivityMainBinding.java. If we have a layout called activity_map.xml, its class name will be ActivityMapBinding.java.
8- An id is assigned to each view.
9- There is a View Group in the project layout of ConstraintLayout and a TextView.
10- To be able to access the views, we must assign an id to them. Like the following:
11- The codes that will be placed in the activity_main.xml section are as follows:
12- Then we need to define an object inside the ActivityMainBinding class so that the views can be accessed directly.
13- Then we will see that the ActivityMainBinding class can be identified by Android Studio, if this class has been created and can be used.
14- The code that should be included in the MainActivity.java section is as follows:
In the code above, select the name of your choice for mainBinding. Then, before setContentView and inside the onCreate method, we value it using the inflate method. Then we can access the View Group and all views that have an id.
15- Move getRoot to the setContentView method to access the layout using the Binding object.
16- Replace getlay with R.layout.activity_main. Like the following
17- Now we print the text on txt_view using the setText method so that we can test the View Binding. Like the following:
18- We run the project on the simulator.
19- As a result of the project execution, we will see that the layout and text written inside setText will be displayed.
20- Then add a Button to the layout.
21- A setOnClickListener ctivity_main.xml are as follows:
22- The code in the MainActivity.java section is as follows:
23- We will run the project again.
How can we access the contents of the View Binding classes?
24- It should be noted that if you want to access the contents of the View Binding classes from the beginning, you must select the Rebuild Project option in the Build tab when building the project.
25- To be able to easily view the contents, you must set how the project is displayed on Project or Project Files.
26- The codes that should be in the ActivityMainBinding.java section are as follows:
In the code above, @NonNull is specified for each of the variables, which also means that we have an activity_main.xml related to Landscape, and we delete or add one of the views, instead @NonNull we need to enter @Nullable.
Like the following codes:
27- Then we add a new layout to the project called layout_bottom.xml and then define a TextView with the btm_txt ID inside it. 28- The codes that should be in the layout_bottom.xml section are as follows:
29- Then we add the layout in activity_main.xml using the include tag.
30- In this part of the project, select the include_layout ID.
31- The codes that should be in the activity_main.xml section are as follows:
32- To be able to access btm_txt in layout_bottom, we do the following.
includeLayout is the include_layout ID.
33- Then we can use the setText method for TextView. Like the following:
The class created for layout_bottom.xml is also called LayoutBottomBinding.java.
34- If we run the project, we will see that the TextView text also changes.
How can we disable View Binding for a specific layer?
Several layouts in the project may require the View Binding class and some may not, in which case we can disable View Binding for some layouts that do not.
35- To do this, just add the following code to the layout tag:
In this project, for example, we have created a layer called layout_top.xml, which because its content is static, we want to disable View Binding.
36- The codes that should be included in the layout_top.xml file are as follows:
37- Add a TestFragment to the project.
38- Then in the layout which is fragment_text.xml, we define a TextView with a unique fragment_txt ID.
39- The code that should be in the fragment_test.xml section is as follows:
40- Create an object from the View Binding class for the layout.
41- The code that should be in the TextFragment.java section is as follows:
In the code above, we first created a sample from the FragmentTestBinding class, which we also named frgBinding.
Then we removed the main inflate and defined the inflate that was related to View Binding.
42- For TextView, we define the setText method in onCreateView before return. Like the following:
43- We also define the fragment itself in activity_main.xml, but we must assign the id to the fragment tag.
44- The code to be included in the activity_main.xml section is as follows:
45- We are implementing the project.
46- After execution, we will see that the text we wrote inside setText has replaced the text in TextView.About our Android and iOS app development services