Native Mobile App Development With Java

View Binding Example

24-Sep-2024

View Binding Example


1/  Open build.gradle file ( app module) and add the following code in android block

    

   in my case the gradle is in Kotlin that's why the coding format is like this. If you choose Grovy in your project then the format will be a little different 





buildFeatures{
viewBinding = true
}



below is the full code of gradle file 



plugins {
alias(libs.plugins.android.application)
}

android {
namespace = "com.example.viewbingingexample"
compileSdk = 34

defaultConfig {
applicationId = "com.example.viewbingingexample"
minSdk = 26
targetSdk = 34
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildFeatures{
viewBinding = true
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}

dependencies {

implementation(libs.appcompat)
implementation(libs.material)
implementation(libs.activity)
implementation(libs.constraintlayout)
testImplementation(libs.junit)
androidTestImplementation(libs.ext.junit)
androidTestImplementation(libs.espresso.core)
}


After changes to gradle file, you must sync your project.




2/ Modify your activity_main.xml file. Add an ID on text view;



<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>



3/ Modify your MainActivity.java file





package com.example.viewbingingexample;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.example.viewbingingexample.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);

ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());

setContentView(binding.getRoot());

binding.textView.setText("Text Set Via view binding");

}
}


Here first you should bind your XML file.

With the view binding, automatically create a file with the same name as the XML file just adding the Binding last of the XML file

in the above example, the XML file name is activity_main
and view binding create a file name ActivityMainBinding

after getting the XML views you should set the view in the setContentView method 




setContentView(binding.getRoot());



after that, you can use views directly by binding variables without creating views variables and initializing by findViewById



binding.textView.setText("Text Set Via view binding");


Output:



Comments