Moment For Technology

JetPack Study Notes DataBinding (1)

Posted on Oct. 14, 2023, 6:40 p.m. by Dean Wright
Category: android Tag: Android Jetpack

JetPack Study Notes DataBinding (1)

The layout file is usually only responsible for the layout of UI controls. The page associates the layout file with the setContentView() method, then finds the control by the View ID, and then acts on the View in code. You can say that the page (Activity or Fragment) does most of the work. The advent of DataBinding allowed the layout file to do some of the work that used to belong to the page, and further decoupled the page from the layout file.

Advantages of DataBinding:

  • Projects are more concise and readable. Some of the work related to the UI controls can be done in the layout file.
  • The findViewById() method is no longer needed.
  • A layout file can contain simple business logic. Views can be directly bound to fields in the data model and can even respond to user interactions.
DataBinding is inseparable from the MVVM architecture. In fact, DataBinding was designed by Google to better implement the MVVM architecture for Android.
Use DataBinding to complete simple DataBinding
1. Enable data binding in your app's build.gradle file
android {
    dataBinding {
        enabled = true
Copy the code
2. Convert the layout file to a Data Bind layout. For details, see the following figure.

The modified layout file is

? The XML version = "1.0" encoding = "utf-8"?  layout xmlns:android="" xmlns:tools="" xmlns:app="" data /data LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".databinding.DataBindingActivity" /LinearLayout /layoutCopy the code

As you can see, a Layout tag has been added to the outermost layer of the layout file, and the namespace has been moved to the Layout tag. The data tag is described later.

At this point, the DataBinding library is automatically generated to bind the required classes to the layout file

3. Create data objects
/** * public class Book {private String name; private String title; private String money; public Book(String name, String title, String money) { = name; this.title = title; = money; } public String getName() { return name; } public void setName(String name) { = name; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getMoney() { return money; } public void setMoney(String money) { = money; }}Copy the code
4. Introduce objects into the layout file and bind layout variables to member variables in the data model
? The XML version = "1.0" encoding = "utf-8"?  layout xmlns:android="" xmlns:tools="" xmlns:app="" data variable name="book" type="com.example.jetpackpro.databinding.Book" / /data LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".databinding.DataBindingActivity" TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@{}"/ TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@{book.title}"/ TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@{}"/ /LinearLayout /layoutCopy the code
5. Instantiate the layout file in the page
public class DataBindingActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_data_binding); ActivityDataBindingBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_data_binding); Book Book = new Book(" This is the title "," this is the author ","20 yuan "); binding.setBook(book); }}Copy the code

After running, the page is displayed as:

6. Simple mapping of fields in the data model in the layout file.

The View display values in the layout file can not only simply display the values in the data model, but can also use external classes to do some processing. Let's say we generate a class that handles money.

Public class MoneyHelp {public static String} /** * formatMoney(String money){ return money + "$"; }}Copy the code

Introduce this class into the layout file and change the View that shows money to:

        import type="com.example.jetpackpro.databinding.MoneyHelp"/

Copy the code

Change the money in the Activity initializing the Book object to 20 and rerunning the project:

About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.