Contents of this series

  • Introduction to using Data Binding (1)
  • Integration and configuration using Data Binding (2)
  • Use Data Binding (3) in activities
  • Use Data Binding (4) in fragments
  • Use Data Binding (5)
  • Use Data Binding (vi) RecyclerView Adapter for use
  • Use BindingAdapter to simplify image loading
  • Using Data Binding (8) Using custom Interface
  • A workaround where Data Binding Android Studio cannot properly generate related classes/methods

Modifying the Activity layout

If the Activity supports Data Binding, add the “< Layout >” tag to the outermost layer of the layout. Since it is added to the outermost layer, the changes are very simple and will not affect the existing layout structure even if you refactor the existing project.

The following uses MainActivity as an example.

Modify the layout of activty_main.xml before:

<?xml version="1.0" encoding="utf-8"? >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    >

    <TextView
        android:id="@+id/tv_example"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        />

</LinearLayout>
Copy the code

The layout of activty_main.xml after modification:

<?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">

    <data>

    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center"
        >

        <TextView
            android:id="@+id/tv_example"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp" />

    </LinearLayout>
</layout>
Copy the code

The element in the tag contains the data required by the page, which will be covered in later articles. You can also click here to view it. Here we will focus on the use of data Binding in the Activity.

Bind in the Activity

Now that we’ve modified the layout above, we can do data binding in our code. The project automatically generates the Java binding file for the layout :ActivityMainBinding. If you look closely, you’ll see that the file name translates the layout’s underscore form into the Java specification’s hump form, followed by a Binding.

Let’s go to MainActivity.java and do the data binding.

If you find that the IDE does not automatically generate the corresponding Binding class correctly while writing code, please refer to this article :Android Studio can’t generate the related classes/methods properly.

  1. Define member variables:
private ActivityMainBinding mBinding;
Copy the code
  1. Initialize the mBinding in onCreate()
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
    mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    mBinding.tvExample.setText("Binding Text");
}
Copy the code

All views in the layout can now be retrieved from mBinding, such as mbinding.tvExample above.

conclusion

Using a Data Binding in an Activity is easy, eliminating the need for template-based findViewById() code and using third-party libraries such as ButterKnife, saving time and effort.

In addition to getting the elements in the layout, we’ll cover how to set the data in the layout in future articles, which can be viewed here.

For more questions, please refer to my other Android-related blog: My blog address