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

Example
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

 

buildFeatures{
    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 'com.android.application'
    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 "android.support.test.runner.AndroidJUnitRunner"
    }

    buildFeatures{
        viewBinding true;
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    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 'com.android.support.constraint:constraint-layout:2.0.4'
   
}

 

sample activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/txt_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

 

MainActivity.kt

package com.rrtutors.moviereview

import android.support.v7.app.AppCompatActivity
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?) {
        super.onCreate(savedInstanceState)
        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 :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

923 Views