Android View Binding | What is View Binding and View Binding Example

Published March 09, 2021

In this post we will cover what is View Binding in android and how to implement view binding in android.

View Binding is a technique which will allows us to write code to easy access the view elements from the xml files.
Previuosly to access views inside the class we used findViewById() method and access the view elements.

After that Kotlin introduced kotlin synthetics, By using this synthetics  we will write the view name to access inside the class

If we have a textview id as txt_title, then we could use it inside class  directly calling them by their id names

But this kotlin synthetics was deprecated or will be deprecated or included in your android studio
projects by default anymore and the problem with this approach with kotlin synthetics was that even though we could call these views immediately by their id all of

these could potentially be null so we could access views that actually don't belong to the current activity layout

For example we could access views in the activity that views actually are not contained in the current activiyt_main layout xml file here is the broblem solve by view binding

View Binding basically  access in an activity or fragmnet.


How to enable View Binding in Android?

To enable View Binding in android project we need to enable buildFeature inside the build.gradle file


    viewBinding true;


By enabling view bindig it will create a Binding class with the name of the Current Activity.

In our example our activity name is MainActivity so by enabling view binding the MainActivityBinding class will be created and it will access all view elements directly.


Create Binding Instance by

Access elements by the binding instance as below

lateinit var binding: ActivityMainBinding
binding= ActivityMainBinding.inflate(layoutInflater)


And then access the views by this instance variable

binding.txtTitle.text="View Binding Example"


Android view Binding Example

build.gradle file

plugins {
    id ''
    id 'kotlin-android'

android {
    compileSdkVersion 30
    buildToolsVersion "29.0.3"

    defaultConfig {
        applicationId "com.rrtutors.moviereview"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner ""

        viewBinding true;
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), ''
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    kotlinOptions {
        jvmTarget = '1.8'

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation ''


sample activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
< xmlns:android=""

        android:text="Hello World!"
        app:layout_constraintTop_toTopOf="parent" />




package com.rrtutors.moviereview

import android.os.Bundle
import android.viewbinding.ViewBinding
import com.rrtutors.moviereview.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        binding= ActivityMainBinding.inflate(layoutInflater)
        binding.txtTitle.text="View Binding Example"

 Related Topics

How do i use Navigationview in android with Kotlin Android ViewModel Interview questions


Article Contributed By :