Using Android Data Binding in 2 steps with a simple way to make easier coding

Abhishek Srivastava
2 min readSep 27, 2020

--

Hi Reader,

The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically.

Layouts are often defined in activities with code that calls UI framework methods. For example, the code below calls findViewById() to find a TextView widget and bind it to the userName property of the viewModel variable:

Java:

TextView textView = findViewById(R.id.sample_text);
textView.setText(viewModel.getUserName());

Kotlin:

findViewById<TextView>(R.id.sample_text).apply {
text = viewModel.userName
}

So Reader We can start

First Step:

1: Enable data binding in your Android application

Go to the app/build.gradle and add the below code

android {
...
...
dataBinding {
enabled = true
}
}

Now the time of step 2

2: Go to your any .xml file in the layout folder

I show you one example

I create activity_main.xml and I can use data-binding here. So that I add the below code in my XML file.

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

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

<
include
android:id="@+id/toolbar"
layout="@layout/toolbar" />


</
RelativeLayout>
</layout>
  • The layout should have <layout> as the root element. Inside <layout> the usual code of layout will be placed.
  • Once data binding is integrated into the layout file, goto Build -> Clean Project and Build -> Rebuild Project. This will generate necessary binding classes.
  • The generated binding classes follow the naming convention considering the layout file name in which binding is enabled. For the layout activity_main.xml, the generated binding class will be ActivityMainBinding (Binding suffix will be added at the end).

Almost done

Now you can call any id of activity_main.xml in your activities like this

IN-ACTIVITY

public class MainActivity extends AppCompatActivity {
private
ActivityMainBinding binding;

@Override
protected void onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
/**
* No need to add this below line so that you can remove
*/
// setContentView(R.layout.activity_main);

/**
* Here the use of data-binding
*/
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

/**
* Now you can access all id here I show you one example below
*/
binding.textView.setText("Binding is working");
}
}

IN-FRAGMENT

public class MainFragment extends AppCompatActivity {
private
ActivityMainBinding binding;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
/**
* No need to add this below line so that you can remove
*/
// setContentView(R.layout.activity_main);

/**
* Here the use of data-binding
*/
binding = DataBindingUtil.inflate(inflater, R.layout.activity_main, container, false);

/**
* Now you can access all id here I show you one example below
*/
binding.textView.setText("Binding is working");
/**
This is required to return binding with getRoot
*/ return binding.getRoot();
}
}

For more references

check out the official website: https://developer.android.com/topic/libraries/data-binding

— — — — — — Now everything is finished. — — — — —

Conclusion

Those are not all the advantages of the DataBiding library but it’s time to end this article. I hope we have sparked your interest in this library, you have learned something new and will put this knowledge into practice.

Hope you learn

Thanks for reading!

Special Thanks to ALL ❤️

--

--

Abhishek Srivastava

I am Software Developer having Java, Java Spring boot Rest services, Android, Kotlin, Flutter skills and Android SDK libraries...