Tech Blog

Coding

findViewById() vs DataBinding

By Satyabrata Dash2 min read
← Back to blog

We are all familiar with the findVIewById() method but it is a costly operation for applications. Each and every time we try to do findVIewById() that particular view is being searched in the view hierarchy during runtime which is a time consuming process and it can slow the application for the user.

To fix it we can go with Data Binding. Data Binding allows us to connect to a layout to an activity or fragment at compile time. The compiler generates a helper class called a binding class when an activity/fragment is created and then by using that binding class object we can access views easily and perform operations on it.

Dependencies:

To enable data binding in our project add below lines in our app level gradle

android {
    ...
    buildFeatures {
        dataBinding true
    }
}

How to implement:

Use better pointers

  1. Surround the layout xml code in <layout></layout> and add the namespace declarations to the tag.
Ex:
<layout 
xmlns:android="http://schemas.android.com/apk/res/android"
   		xmlns:app="http://schemas.android.com/apk/res-auto">
		<LinearLayout
   			android:layout_width="match_parent"
   			android:layout_height="match_parent"
   			android:orientation="vertical">
		<TextView
   			android:id="@+id/score_text"
   			android:layout_width="wrap_content"
   			android:layout_height="wrap_content"/>

		</LinearLayout>	    
</layout>

  1. Get a data binding object in our activity/fragment by calling
DataBindingUtil.setContentView() for activity DataBindingUtil.inflate() for fragment.

val dataBindingObject  = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)

Note: Let’s say the name of the activity is MainActivity then the binding class for the activity will be ActivityMainBinding.

  1. Now by using the binding object we can call the views directly.
Ex: 
dataBindingObject.scoreText = View.VISIBLE

class MyScoreDetailsFragment : Fragment()
{
override fun onCreateView(inflater: LayoutInflater,   container: ViewGroup?, savedInstanceState: Bundle?): View?
{
		val binding: MyScoreDetailsFragment = DataBindingUtil.inflate(inflater, R.layout.myscoredetails_fragment, container, false)
binding.scoreText.text = "score"
return binding.root
}
}

Basically with data binding enabled the compiler creates a reference to all views in a <layout></layout> that have an id and gathers them in a binding object and then we just use the object to perform operations on them.

Keep reading

You may be interested in

Browse all articles →
Active Directory Federation Service
July 5, 20235 min read

Active Directory Federation Service

Active Directory Federation Service (ADFS) uses claims-based identity model to maintain application security and to implement federated identity. ADFS is a concept of Windows Server operating system, that provides users with single sign-on access to the application, those are located across organizational boundaries. For Example: We have 2 organizations – Organization 1 and Organization 2. […]

By Abinash MishraRead More →
Remote Access to Microsoft SQL Database Server
April 5, 20232 min read

Remote Access to Microsoft SQL Database Server

Remote access to a SQL server is important because it allows users to access the server from anywhere, at any time. This enables remote workers to access the data they need when they need it, even if they are not physically in the same location as the server. It also allows IT admins to access […]

By Malkeet SinghRead More →
Types of Load Balancer in AWS
February 6, 20235 min read

Types of Load Balancer in AWS

What is Load Balancing ? Load balancing is a critical component of any scalable and highly available Cloud or On-premise architecture. It helps distribute incoming traffic across multiple servers, improving the availability and scalability of a website or application. In the cloud, load balancing is offered as a service by various cloud providers such as […]

By Keerthik ShenoyRead More →