Native Mobile App Development With Java

Data Binding Example

24-Sep-2024

Data 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{
dataBinding = true
}



below is the full code of gradle file 



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

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

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

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildFeatures{
dataBinding = 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/ create java class: User





package com.example.databindingexample;


public class User {
private String name;
private String email;

public User(String name, String email) {
this.name = name;
this.email = email;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}




3/ Modify your activity_main.xml file. 

  

   3.1/ Convert root layout into layout tag
   3.2/ Add data tag
   3.3/ Add a variable in the data tag ( give a variable name and type ) in the example create a User model and set the model in the variable tag.

   3.4/ Where do we need to set the user data we use this syntax @{user.name} here user is the model and name is the field of this model



<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

<data>
<variable
name="user"
type="com.example.databindingexample.User" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:orientation="vertical"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"
android:textSize="18sp"
android:textStyle="bold"
/>
<TextView
android:layout_marginStart="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.name}"
android:textSize="18sp"
/>
</LinearLayout>
<LinearLayout
android:layout_marginTop="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email:"
android:textSize="18sp"
android:textStyle="bold"
/>
<TextView
android:layout_marginStart="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.email}"
android:textSize="18sp"
/>
</LinearLayout>

</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>


</layout>



4/ Modify your MainActivity.java file





package com.example.databindingexample;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.example.databindingexample.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

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


ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

User user = new User("Test User", "test.user@example.com");

binding.setUser(user);



}
}


Here first you should bind your XML file.

With the data 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 binding the views we can bind data source

in the example user object is bind with the views data variable 



User user = new User("Test User", "test.user@example.com");
binding.setUser(user);




Output:





Comments