Databinding allows binding UI components in layouts to data sources in an Android app. The databinding library automatically generates classes to bind views to data objects. When data changes, bound views are automatically updated. To use databinding, enable it in build.gradle and add binding variables to layout XML. Generated binding classes provide methods to set data and callbacks. Databinding can also be used with RecyclerView by generating item bindings and setting an adapter.
IAC 2024 - IA Fast Track to Search Focused AI Solutions
How to use data binding in android
1. How to Use Data Binding in Android
Databinding is a part of android-architecture components in an android jetpack. It is
a support library and It is used to bind UI components in the layout to data resources in your
app. In another word, we can say that Databinding allows you to write expressions that connect
variables to the views in the layout.
Data Binding Library automatically creates the classes required to bind the views in the layout
with your data objects. Databinding is useful for reducing boilerplate code from the source
code that is usually written to sync the UI when new data is available. When the data changes
its value, the elements that are bound to the data reflect changes automatically.
Which types of devices can use data binding?
Devices running Android 4.0 (API level 14) or higher can be used data binding.
How to use data binding in an android project?
Step 1:
In build.gradle of the app module
1
2
3
4
5
6
7
8
9
android {
.....
.....
dataBinding {
enabled = true
}
}
2. Step 2:
In creating a class for binding view
1
2
3
4
5
6
7
8
9
public class Shape {
public String name;
public String formula;
public Shape(String name, String formula) {
this.name = name;
this.formula = formula;
}
}
For Callback:
1
2
3
4
5
package com.example.databindingdemo;
public interface ViewClickListener {
void onViewClick(Object view, Object model);
}
Step 3:
Add the binding class in XML layout.
fileName activity_main.xml
11. 52
53
54
</androidx.cardview.widget.CardView>
</layout>
After that, a class is generated automatically with the name of item_layout
i.e. ItemLayoutBinding. In this class, all methods are generated from the variable name in the
.xml file.
So there are two methods in ItemLayoutBinding
1. setItem
2. setItemClickListener
Step 4:
Create an adapter for recycler view items.
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.example.databindingdemo;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import com.example.databindingdemo.databinding.ItemLayoutBinding;
12. 14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class ItemAdpter extends RecyclerView.Adapter<ItemAdpter.ItemViewHolder> {
private Context context;
private String name[] = {"John", "Jelina", "Jonathan", "Marchel", "Richy"};
private String age[] = {"23", "26", "64", "20", "22"};
public ItemAdpter(Context context) {
this.context = context;
}
@NonNull
@Override
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ItemLayoutBinding itemLayoutBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
R.layout.item_layout, parent, false);
return new ItemViewHolder(itemLayoutBinding);
}
@Override
public void onBindViewHolder(@NonNull final ItemViewHolder holder, final int position) {
final UserDetail userDetail = new UserDetail(name[position], age[position]);
holder.bindItem(userDetail);
holder.itemLayoutBinding.setItemClickListener(new ViewClickListener() {
@Override
public void onViewClick(Object view, Object model) {
UserDetail user = (UserDetail) model;
Toast.makeText(context, "Hello " + user.name + ", You are " + user.age + " year old.",
Toast.LENGTH_SHORT).show();
}
});
13. 44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
}
@Override
public int getItemCount() {
return name.length;
}
public class ItemViewHolder extends RecyclerView.ViewHolder {
private ItemLayoutBinding itemLayoutBinding;
public ItemViewHolder(@NonNull ItemLayoutBinding itemView) {
super(itemView.getRoot());
this.itemLayoutBinding = itemView;
}
public void bindItem(UserDetail item) {
itemLayoutBinding.setItem(item);
}
}
}
Step 5:
Init Adapter in Activity class
1
2
3
package com.example.databindingdemo;
import android.os.Bundle;
14. 4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import com.example.databindingdemo.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding activityMainBinding;
private ItemAdpter itemAdpter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
initialisedRecyclerView();
}
private void initialisedRecyclerView() {
itemAdpter = new ItemAdpter(this);
activityMainBinding.setItemAdapter(itemAdpter);
}
}
You can also read our blogs on Android:
1: Android Navigation Component – Android Jetpack
2: Offline Content Storage and Sync with Server when Modified
3: Custom Camera using SurfaceView
15. 4: Capture Image on Eye Blink
InnovationM is a globally renowned Android app development company in India that caters to a
strong & secure Android app development, iOS app development, hybrid app development
services. Our commitment & engagement towards our target gives us brighter in the world of
technology and has led us to establish success stories consecutively which makes us the
best iOS app development company in India. We are also rated as the best Mobile app
development company in India.
Thanks for giving your valuable time. Keep reading and keep learning 🙂