View Binding in Android

Shashi Kant
Android Tutorials

--

We have learnt that every time we need to access a view from our XML layout into our Java or Kotlin code, we must use findViewById(). It was okay for small/personal projects where we use 5 to 6 views in a layout. But for larger projects we have comparatively more views in a layout, and accessing each view using the same findViewById() is not really comfortable.

What is View Binding?

View binding is a feature that allows you to more easily write code that interacts with views. Once view binding is enabled in a module, it generates a binding class for each XML layout file present in that module

Simply put, this allows us to access the views from the layout file in a very simple way by linking variables of our Kotlin or Java code with the XML views. When a layout is inflated, it creates a Binding object, which contains all the XML views that are casted to the correct type. This makes it really easier for us since we can retrieve all views in one line of code.

Getting started with View Binding

  1. Let’s start by enabling view binding in our project:

In build.gradle(:app) add the code in -> android { … }

android {
buildFeatures {
viewBinding = true
}
}

2. Before the onCreate() method, we create our binding object

lateinit var binding: ActivityMainBinding

3. Perform the following steps in onCreate() method:

  • Call the inflate() method to create an instance of the binding class for the activity to use.
  • Get reference to the root view
  • Pass the root view to setContentView() [setContentView(binding.root)] instead of layout [setContentView(R.id.activity_main)]
lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}

4. To get reference of any view, we can use the binding object:

binding.buttonResult.setOnClickListener { }
val cost: String = binding.editTextCost.text.toString()

Using View Binding in Fragments

We follow the same steps:

  1. Before the onCreateView() method, we create our binding object
private var binding: MyFragmentBinding

2. Initialize our binding object in onCreateView()

binding = MyFragmentBinding.inflate(inflater,container,false);
return binding.getRoot();

3. To get reference of any view, we can use the binding object

binding.aboutBtn.setOnClickListener {
Toast.makeText(activity, “Hello”, Toast.LENGTH_SHORT)
.show()
}

Note: The name of the binding class is generated by converting the name of the XML file to camel case and adding the word “Binding” to the end. Similarly, the reference for each view is generated by removing underscores and converting the view name to camel case. For example, activity_main.xml becomes ActivityMainBinding, and you can access @id/text_view as binding.textView.

View binding has important advantages over using findViewById():

  • Null safety: Since view binding creates direct references to views, there’s no risk of a null pointer exception due to an invalid view ID.
  • Type safety: The fields in each binding class have types matching the views they reference in the XML file. This means that there’s no risk of a class cast exception.

Start using View Binding

If you’re intrigued by View Binding and want to learn more about it, here’s some resources for you to learn:

Documentation : [Click Here]

One-Liner ViewBinding Library

You would have noticed that to use View Binding, we need to call the static inflate() method included in the generated binding class ( which creates an instance of the binding class for the activity or fragment )

Yesterday I came across an awesome library that makes ViewBinding one-liner ( By removing the boilerplate code and easily set ViewBindings with a single line )

One-liner ViewBinding Library : [Click Here]

Developed by : Abhinav

For more updates, follow: @shashi-kant10

Article by : Shashi Kant

--

--

Shashi Kant
Android Tutorials

Hi 👋, I’m Shashi. A passionate Android developer from India.