How to use ObservableArrayMap class in Android DataBinding

In this android programming source code example, we are going to use ObservableArrayMap class in Android DataBinding.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to use ObservableArrayMap class in Android DataBinding.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout 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">
    <data>
        <import type="androidx.databinding.ObservableMap" />
        <variable
            name="country"
            type="ObservableMap<String, Object>"/>
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="30dp">

        <TextView
            android:id="@+id/countryName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text='@{country["name"]}'
            android:gravity="center"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>


    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableArrayMap;

import android.os.Bundle;

import com.bluapp.androidview2.databinding.ActivityFirebaseMlKitAndDataBinding14Binding;

public class FirebaseMlKitAndDataBindingActivity14 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ObservableArrayMap<String, Object> country = new ObservableArrayMap<>();
        ActivityFirebaseMlKitAndDataBinding14Binding binding = DataBindingUtil.setContentView(this, R.layout.activity_firebase_ml_kit_and_data_binding14);
        binding.setCountry(country);
        country.put("name", "Nigeria, Sweden, India e.t.c");
    }
}

app/build.gradle

dataBinding {
        enabled = true
    }

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.