Data binding for android beginners.
Data binding is a way to bind the UI components like TextView,EditText….. to your code avoiding boiler plate code for initializing every view.
TextView txtView = findViewById(R.id.txtView);
Data binding is a part of JetPack components, allowing a user to make a faster way to program.
Enabling data binding:
dataBinding{
enabled = true;
}
Declaring in layout :
There is a mechanism to be implemented to get data binding, now include your layout file inside the
<layout>
</layout>
Let’s see a complete instruction
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="User"
type="com.abhishek.databinding.User" />
</data>
<RelativeLayout 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"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:padding="10dp"
android:text="@{User.name}"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:layout_centerHorizontal="true"
android:padding="10dp"
android:text="@{User.age}"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/mobile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/age"
android:layout_centerHorizontal="true"
android:padding="10dp"
android:text="@{User.mobile}"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/mobile"
android:layout_centerHorizontal="true"
android:padding="10dp"
android:text="@{User.email}"
android:textColor="@android:color/black" />
</RelativeLayout>
</layout>
Now the crucial step is to generate a Activity Binding class, is automatically generated if you follow the steps this class is important to deal with data bindings
ActivityMainBinding activityMainBinding;
In your MainActivity class file we need to make use of this file and handle our databinding’s available in that particular layout file.
activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
Generate a User model class for accessing the UI objects
public class User {
public String name;
public String age;
public String mobile;
public String email;// Getters and Setters}
Now initialize the class in MainActivity
User user = new User();
Update the details into the Model class
user.setName("Abhishek");
user.setAge("26");
user.setMobile("9876543210");
user.setEmail("abhi@email.com");
then setData to UI components
activityMainBinding.setUser(user);
Advantages
- Easy to implement and use as well.
- Faster than implementing findViewById pattern.
- Reduces boilerplate code.
- Readability of code is improved.
- Helpful in testing scenarios.
- A powerful way to deal with UI components.
For more detailed info visit
http://www.androidcoding.in/2020/02/23/android-data-binding-library-java-jet-pack/