A demo of how to implement Data Binding in Android app

Overview

DataBindingExample

A demo of how to implement Data Binding in Android app



Each Activity in the demo app shows a sample implementation of data binding.

  1. Replace findViewById with data binding

Step 1: In layout file, make <layout> tag as most parent tag or root tag. After adding it, build system will process it for data binding.
Step 2: After above step, binding class will be generated based on same name of layout file (e.g. activity_main’s binding class will be generated as ActivityMainBinding). Set setContentView using DataBindingUtil.
That’s it.
Example: DataBinding1Activity

  1. How can we change binding class name?

By default, binding class is generated based on the name of the layout file and placed in a data binding package under the module package. E.g. the layout file activity_main.xml will generate ActivityMainBinding. If the module package is com.an.demo, then it will be placed in com.an.demo.databinding directory.
We can change that by adding this tag <data class="MainActivityBinding"> to the layout file.
Now generated binding class is named MainActivityBinding
That’s it.
Example: DataBinding2Activity

  1. How to bind data in layout file?

Step 1: Create a POJO class i.e. User
Step 2: Add element to the layout file. element is a way to binding data with UI element.
Step 3: Add element to the layout file. is a object of POJO which we bind with UI.
Step 4: add @{user.name} in text property of TextView. Generally, Expressions within the layout are written in the attribute properties using the @{} syntax
Step 4: Set binding.setUser(user); in the Activity class
That’s it.
Example: DataBinding2Activity

  1. Event Handling using data binding

Step 1: In layout file, make <layout> tag as most parent tag or root tag.
Step 2: After above step, binding class will be generated based on same name of layout file (e.g. activity_main’s binding class will be generated as ActivityMainBinding). Set setContentView using DataBindingUtil.
Step 3: Create a POJO class i.e. LoginModel and add it as a variable to the layout file.
Step 4: Create a presenter class i.e. LoginPresenter and add it as a variable to the layout file.
Step 5: android:onClick="@{presenter::onClickButton2}" is an example of event binding with Method reference That’s it.
Example: MainActivity

  1. Custom Data Binding Implementation

Step 1: In layout file, make <layout> tag as most parent tag or root tag. Create a POJO class i.e. LoginModel and add it as a variable to the layout file. i.e. LoginModel. Create a presenter class and add it as a variable to the layout file. i.e. LoginPresenter
Step 2: Create a separate adapter class for Binding i.e. DataBindingAdapter. Functionality of the custom binding class would be to display a Toast message. Create the binding method inside the customer adapter class.i.e.

    public static void showToast(View view, String toast) {
       if(toast != null && !toast.isEmpty()) {
           Toast.makeText(view.getContext(), toast, Toast.LENGTH_SHORT).show();
       }
   } 

Add the custom attribute to your view in the layout file.i.e. app:toast="@{loginInfo.loginMessage}"
Step 3: Set binding.setModel(new LoginModel()); in the Activity class.
Step 4: Set binding.setPresenter(new LoginPresenter()); in the Activity class.
That’s it.
Example: DataBinding3Activity

  1. Implement RecyclerView using Data Binding

Step 1: In layout file, make <layout> tag as most parent tag or root tag.
Step 2: Create a custom BindingViewHolder class for reusability purposes.
Step 3: Create your recyclerview adapter class and item layout class. Set data & variable tag to the layout item class.
Step 4: Set binding.recyclerView.setAdapter(adapter); to the Activity class
That’s it.
Example: DataBinding4Activity

You might also like...
The goal of this assignment is to implement GameOfFifteen
The goal of this assignment is to implement GameOfFifteen

Game of Fifteen Demo The board for the game of Fifteen is filled randomly with numbers from 1 to 15 and one empty space. You can move the neighboring

Learning about architecture with implement TMDB Restful API

PopCorn Movie and Tv Show list application. Build for learning about architecture (maybe it's clean architecture, but i don't know it's clean or not.

Implement Text Recognition using MLKit in Jetpack Compose using Kotlin
Implement Text Recognition using MLKit in Jetpack Compose using Kotlin

Implement Text Recognition using MLKit in Jetpack Compose using Kotlin. The application is built using Kotlin Programming Language with Jetpack Compose Navigation.

This repo contains sample mobile apps that implement contributor design pattern

This repo contains sample mobile apps that implement contributor design pattern. This design pattern was evolved to establish clean contracts for the developers and partners in Teams Mobile code base

Book Parking is a demo application based on MVVM architecture. The app allows users to booking parking slots, the app uses firebase for the backend.
Book Parking is a demo application based on MVVM architecture. The app allows users to booking parking slots, the app uses firebase for the backend.

Book Parking is a demo application based on MVVM architecture. The app allows users to booking parking slots, the app uses firebase for the backend.

The AboutMe app - a demo app that shows information about a person
The AboutMe app - a demo app that shows information about a person

AboutMe The AboutMe app is a demo app that shows information about a person. Name Settable Nickname An image Scrollable information This app demonstra

The SleepQualityTracker app - a demo app that helps you collect information about your sleep
The SleepQualityTracker app - a demo app that helps you collect information about your sleep

The SleepQualityTracker app is a demo app that helps you collect information about your sleep. This app builds on the SleepQualityTracker previous made, refactoring the code to make it more efficient so it will be easier to maintain and test

Android Demo App for League of legends's Champions based on MVVM design pattern
Android Demo App for League of legends's Champions based on MVVM design pattern

🎉 LOL-Champs If this project is useful, please give it a star ⭐ A Android Sample App with champion information of the League of Legends(LOL) using An

Marvel API Demo App

Marvel API Demo app How to set up the API Keys OPEN FILE: gradle.properties ADD (using valid public/private keys): MARVEL_PUBLIC_KEY = "" MARVEL_PRIV

Owner
Anitaa Murthy
Android Developer/iOS Developer
Anitaa Murthy
CMPLR Technologies 8 Apr 5, 2022
Android-splash-screen-demo - Sample application to demo the various features provided in android-splash-screen

Android Splash screen API demo This is a sample application used to demonstrate the various features provided in android-splash-screen. More details c

Sridhar 1 Jan 3, 2022
OpenWeatherMap-API-Demo - Demo Android Application for OpenWeatherMap API

WeatherForecast Demo Android Application for OpenWeatherMap API Table of Content

Rashid Hussain 0 Jul 10, 2022
A Kotlin binding to webview, a tiny cross-platform webview library, supports Java and Native.

webviewko provides a Kotlin/JVM and a Kotlin/Native(experimental) binding to webview, a tiny cross-platform webview library to build modern cross-platform GUIs using WebView2, WebKit and WebKitGTK.

Winterreisender 17 Dec 30, 2022
A demo project showcasing different exercises + details with prepopulated data from room. Includes tests

README Pre-requisites I used Android Studio Bumblebee (2021.1.1). In case of any issues, do let me know because I understand that issues might vary fo

Carol 24 Nov 17, 2022
Implement Dog vs Cat Prediction Model in Android app

This project aims to classify the input image as either a dog or a cat image. The image input which you give to the system will be analyzed and the predicted result will be given as output. Machine learning algorithm [Convolutional Neural Networks] is used to classify the image.

Aditya deshmukh 6 Oct 27, 2021
Implement Webpage in Android App

This is the application for add a webpage in a android app and act like a browser

Ankit P Chandran 1 Oct 14, 2021
Happy-Birthday - Design and implement a single screen app that displays information

Happy Birthday Android App | Android Basics in Kotlin Course Solution code for t

Anas Tariq 1 Feb 6, 2022
📱 Android Library to implement Rich, Beautiful, Stylish 😍 Material Navigation View for your project with Material Design Guidelines. Easy to use.

Material NavigationView for Android ?? ?? Android Library to implement Rich, Beautiful Material Navigation View for your project with Material Design

Shreyas Patil 198 Dec 17, 2022
Sample to show how to implement blur graphical tricks

BlurEffectForAndroidDesign Sample to show how to implement blur graphical tricks All the explanations could be found here: http://nicolaspomepuy.fr/?p

Nicolas POMEPUY 2k Dec 28, 2022