This year, google released data binding library. It’s really interesting to use data binding on Android.

Views and View Injection

Normally, when we write codes in activity or fragment, we need to write a lot of boilerplate codes.

urlEditText = (EditText) rootView.findViewById(;
portEditText = (EditText) rootView.findViewById(;
okButton = (Button) rootView.findViewById(;

Before data binding library comes out, I used Butterknife to make code concise and readable. Butterknife injects views into your activity or fragment, so you don’t need to write “getViewById” or  bind some UI events like onClick by yourself.

@Bind( TextView mTextStored;
@Bind( Button mBtnRefresh;

public void onClick(View v) {

Although Butterknife is good enough to avoid boilerplate codes, I personally think data binding is better. Data binding library allows us to bind views and data objects by simply modifying layout files and creating objects. What’s more, you don’t have to write many annotations or resource IDs. Does it sounds interesting?


Before I explain how to implement data binding on Android, I wanna talk about MVVM. What is MVVM? It represents Model-View-ViewModel. Model is the object which represent data. View helps you show data, so it’s basically UI component. ViewModel communicates with Model and View. It helps you to handle UI event and load data. For more information, you can check on the references below this article.


First step of implementation, you’ll need to define Model, just like this one:

public class Phone {
private String name;

public Phone(){  
public Phone(String name){ = name;  
public String getName() {  
    return name;  
public void setName(String name) { = name;  
}   }  

It’s just a normal java bean object, or you can also write POJO.


Define your ViewModel to show data on views.

public class PhoneViewModel {

public final String name;  
public PhoneViewModel(String name) { = name;  
public String getName() {  
    return name;  


This is a very basic view model, and if you want to handle some UI event, you can write in ViewModel as well. Then, put it into layout file. However, we haven’t got there yet. So let’s continue.


Write layout file, and put your ViewModel into layout. The layout xml will be like this one:

In , describes which viewmodel you want to put into this layout, and define the package path in "type" attribute. For example, I put PhoneViewModel into this layout by naming them "viewModel". Also, I put @{} in the TextView to show the model name.

After creating the layout file, we can use them in activity, fragment, or even adapter. In activity, you can get Binding object in this way.

MainActivityBinding binding = MainActivityBinding.inflate(getLayoutInflater());

Or you can get it in adapter…

public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
ItemLayoutBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_layout, parent, false);
return new ViewHolder(binding);

Remember the name of binding object depends on the layout file. For instance, if your layout file, named “item_layout.xml”, the binding object will be “ItemLayoutBinding”. You can reference it respectively.

The code examples of this blog post are available on my GitHub.