As I shown in the previous post where we used BaseObservable and ObservableField to update UI when binding object is changed, this post will show you another simple way to achieve this.

Do Check my previous posts for better understanding of DataBinding and methods used in DataBinding.


Make easy money with infolinks

Here is another way to do it. In this method we will use ObservableField to update the UI.

We will create a Bindable class named User3 which has one ObservableArrayMap member variable.


import android.databinding.ObservableArrayMap;

public class User3 {

    public ObservableArrayMap<Integer, String> name = new ObservableArrayMap<>();


The layout

Lets see how this class is being binded in the layout.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android=""

            type="" />


            android:text="@{}" />

            android:layout_height="wrap_content" />



In the above layout, we have one binding variable named user3 and @{} will get the date from the map which has a 0 as key. Below we will see how we will add data to this Map in java code.

Make easy money with infolinks

Set Data

        ActivityMainBinding binding = DataBindingUtil.
                setContentView(this, R.layout.activity_main);
        // Observable Collections
        user3 = new User3();, "Coderz");, "Heaven");

So In this example the retrieved data will be “Coderz“.

Update the Observable Collection

OnClick of the Button in the layout we will update the values in the UI by changing the object.

 binding.b1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                // Updating  Observable Collections
      , "Heaven");


Please try a Clean & Build if you are facing any issues

Source Code

You can find the full source code in my GitHub repository.

Get the source code.


Leave a Reply

Your email address will not be published. Required fields are marked *




Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.