• Android DataBinding provides a way to tie the UI with business logic allowing the UI values to update automatically without manual intervention. This reduces a lot of boilerplate code in your business logic that you usually write to sync the UI when new data is available.
     

    DataBinding is one of the android architecture components suggested by android.

     Why do we have to use Data Binding?

    Every developer wants clean and understandable code, but creating it is easier said than done. People rush, releases come one after another, clients want results and before you know it, your code is a mess. To overcome these issues data Binding is the best solution.

    Advantage of Data Binding:

    1) Less code :

    By keeping code in activities and fragments small, it helps you write cleaner and more readable code.

    2) Faster apps:

    Since the binding isn’t done in onCreate, your app runs faster.

    3) Safer connection between views and code :

    Because it doesn’t bind at runtime, it’s safer to get the UI components than findViewById().

    4) Easier:

    Since it has less code and causes fewer errors, it’s easier to test and maintain.

    Let's get the brief idea by using Examples:

    1.1 Enabling DataBinding

    To get started with DataBinding, you need to enable this feature in your android project first. Open the build.gradle located under app and enable data binding under an android module. Once enabled, Sync the project and you are good to go.

    app/build.gradle
    android {
        dataBinding {
            enabled = true
        }
     
        compileSdkVersion 31
     
        defaultConfig {
            applicationId "com.info.databinding"
            minSdkVersion 16
            // ..
        }
    }

    1.2 create activity_main.xml layout file

    The earlier activity_main.xml layout we used to create looked like this:

    <RelativeLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:tools="https://schemas.android.com/tools"
        xmlns:android="https://schemas.android.com/apk/res/android">

        <TextView
            android:id="@+id/tvHeading"
            android:layout_width="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_height="wrap_content"
          />


        <TextView
            android:id="@+id/tvSubHeading"
            android:layout_width="wrap_content"
            android:layout_below="@+id/tvHeading"
            android:layout_centerHorizontal="true"
            android:layout_height="wrap_content"
           />
    </RelativeLayout>

    but to use Data Binding we need to add a layout as the root tag. so we need to modify activity_main.xml and it looks like:

    <layout xmlns:android="https://schemas.android.com/apk/res/android"
        xmlns:tools="https://schemas.android.com/tools">

    <RelativeLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:tools="https://schemas.android.com/tools"
        xmlns:android="https://schemas.android.com/apk/res/android">

        <TextView
            android:id="@+id/tvHeading"
            android:layout_width="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_height="wrap_content"
          />


        <TextView
            android:id="@+id/tvSubHeading"
            android:layout_width="wrap_content"
            android:layout_below="@+id/tvHeading"
            android:layout_centerHorizontal="true"
            android:layout_height="wrap_content"
           />
    </RelativeLayout>
    </layout>

    1.3  Create java file i.e MainActivity.java

    pulblic class MainActivity expends AppCompatActivity{
        ActivityMainBinding binding;    

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

            binding.tvHeading.setText("Welcome to Toshal Infotech");
            binding.tvSubheading.setText("You are learning Android Tutorial on DataBinding");
        }

    The ActivityMainBinding class is generated from the XML layout file and it’s named this way because the XML layout file name is turned into Upper CamelCase and appended with Binding

    Note: If the ActivityMainBinding is not available you need to build your project once now so that the auto-generated field names from the layout are available in the MainActivity.java
     

     

0 Years in
Operation
0 Loyal
Clients
0 Successful
Projects

Words from our clients

 

Tell Us About Your Project

We’ve done lot’s of work, Let’s Check some from here