Android Development

Android works with DataBinding

Android DataBinding offers a strategy to hyperlink the consumer interface to enterprise logic, permitting UI values ​​to be up to date mechanically with out handbook intervention. This reduces a variety of normal code in your online business logic that you simply often write to synchronize the consumer interface when new information is out there. DataBinding is likely one of the elements of the Android structure provided by Android.

On this article we are going to study the fundamentals of DataBinding and within the subsequent article, a easy profile display is constructed utilizing the RecycleurVoir with Information hyperlink.

1.1 Activating the information hyperlink
1.2 Primary instance
1.three My DataBinding courses will not be generated?
1.four Linking Information in Layouts
1.5 Liaison clickers / occasion administration
1.6 Replace the consumer interface with the assistance of observables
1.7 Updating the consumer interface with the assistance of ObservableFields
1.eight Loading pictures from a URL (Glide or Picasso)
1.9 Java Hyperlink Features (Imports)

1. Information Hyperlink Fundamentals

1.1 Activating the information hyperlink

To begin utilizing DataBinding, you need to first allow this characteristic in your Android undertaking. Open the construct.gradle situated below app and permit information hyperlink below Android module. As soon as activated, Sync the undertaking and you might be able to go.


compileSdkVersion 27


1.2 Primary instance

Let's say we need to show consumer data from a POJO Consumer class. We often publish the knowledge in a Viewing utilizing Set the textual content () technique. However as an alternative of manually calling setText for every consumer property, DataBinding permits us to bind the values ​​mechanically.

The POJO class beneath creates a Consumer object with title and e-mail tackle.

Public class Consumer

To allow information binding in a presentation, the root the factor ought to begin with label. With, and tags are used.

The next is the construction of the information hyperlink format.

The format ought to have as root factor. Inside the standard format code shall be positioned.
A the tag follows the . All variables and binding strategies should go inside label.
Inside tags, a variable shall be declared utilizing label. The tag variable takes two attributes `nom` and `sort`. attribute title shall be alias title and sort have to be of object mannequin class. In our case, strategy to Consumer class.
To bind a worth, @ the annotation must be used. Within the format beneath, the consumer first title and E-mail are associated to TextView utilizing @ Username and @

As soon as the information hyperlink is built-in within the format file, go to Construct -> Personal Challenge and Construct -> Rebuild Challenge. This may generate the required hyperlink courses.
The generated hyperlink courses respect the naming conference by contemplating the title of the format file wherein the hyperlink is enabled. For the format activity_main.xml, the generated hyperlink class shall be ActivityMainBinding (The hyperlink suffix shall be added on the finish).
To hyperlink the information within the consumer interface, you need to first inflate the hyperlink format utilizing the generated hyperlink courses. Under, ActivityMainBinding inflates the format first and binding.setUser () binds the consumer object to the format.

You possibly can discover right here that we didn’t use findViewById () nowhere.

import android.databinding.DataBindingUtil;
import android.os.Bundle;

import data.androidhive.databinding.databinding.ActivityMainBinding;

Public class MainActivity extends AppCompatActivity

non-public consumer consumer;

Protected void onCreate (Bundle savedInstanceState)

Should you run the applying, you’ll be able to see the main points of the consumer displayed in TextViews.

1.three My DataBinding courses will not be generated?

The present model of Android Studio fails to generate hyperlink courses more often than not. Normally, this may be solved by Cleansing and reconstruction the undertaking. If the issue nonetheless persists, go File ⇒ Disable Cache and Restart. This could finally remedy the issue in case your format recordsdata include no errors.

1.four DataBinding in layouts

As we will use it, we have now not included Coordinator, AppBarLayout and different components within the instance above. Normally, we separate the principle format and the content material into two completely different layouts, particularly: activity_main.xml and content_main.xml. the content_main shall be included in the principle format utilizing label. We are going to now see the way to allow information binding when we have now embedded layouts.

Under we have now activity_main.xml with Coordinator, AppBarLayout and FAB.

the the tag is utilized in activity_main.xml format to allow information binding. As well as, and Tags are used to hyperlink the Consumer object.
To go the consumer to included content_main disposition, bind: consumer = "@ " is used. With out this, the consumer object is not going to be accessible within the content_main format.

the content_main.xml understands once more tag to allow information binding. the , and tags are wanted in guardian and embedded layouts.
android: textual content = "@ consumer.title" and android: textual content = "@ @ consumer.e-mail}" are used to show the information in TextViews.

Public class MainActivity extends AppCompatActivity

Now, in the event you run the applying, you’ll be able to see the information displayed within the included format.

1.5 Liaison clickers / occasion administration

Not solely information, we will additionally hyperlink the clicking and different occasions on consumer interface components. To hyperlink a click on occasion, you need to create a category with the required callback strategies.

Under we have now a category that handles the FAB click on occasion.

Public class MyClickHandlers

To bind the occasion, we use the identical once more tag with the trail to the supervisor class. Beneath android: onClick = "@ " binds FAB to the onFabClicked () technique.

To have an effect on an extended press occasion, the strategy should return boolean sort as an alternative of empty. public boolean onButtonLongPressed () manages the lengthy press view.
You too can go parameters whereas binding. public void onButtonClickWithParam (view, consumer) receives the certain consumer object from the format of the consumer interface. Within the presentation, the parameter may be handed with android: onClick = "@ (v) -> handlers.onButtonClickWithParam (v, consumer)"
To hyperlink occasions, binding.setHandlers (managers) is named exercise.

The instance beneath reveals completely different button click on occasions.

bundle data.androidhive.databinding;

import android.content material.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import data.androidhive.databinding.databinding.ActivityMainBinding;

Public class MainActivity extends AppCompatActivity