[JAVA] (Android) Try to display static text using DataBinding

Introduction

Record it as a study of Data Binding. This time, it's just the beginning of displaying the text.

Introducing Data Binding

Add the following to app gradle

build.gradle


android {
    dataBinding {
        enabled = true
    }
}

Prepare a class (User) to bind

User.java



public class User {
  private String name;
  private String email;

  public User(String name, String email) {
    this.name = name;
    this.email = email;
  }

  public String getName() { return name; }
  public String getEmail() { return email; }
}

layout settings

point

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Binding Objects -->
    <data>
      <!--Named user(Any)Is tied to the User class object-->
      <variable name="user" type="com.example.databinding.User" />
    </data>

    <!-- View -->
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.name}" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{user.email}" />
    </LinearLayout>
</layout>

Bind the object

MainActivity.java


public class MainActivity extends AppCompatActivity {

    private User user = new User("Taro", "[email protected]");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Get an instance of Binding
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        //Associate xml user with MainActivity user
        binding.setUser(user);
    }

in conclusion

Next, I would like to implement a dynamic display. next: Dynamically change text using DataBinding (https://qiita.com/kilalabu/items/3cded5e7c560de2ed05c)

Reference site

https://qiita.com/Omoti/items/a83910a990e64f4dbdf1#step0-%E5%B0%8E%E5%85%A5 Thank you very much

Recommended Posts

(Android) Try to display static text using DataBinding
(Android) Dynamically change text using DataBinding
Try to display prime numbers using boolean type
Try to introduce OpenCV to Android application
Android Development-Try to display a dialog-
[Java] Try to implement using generics
Try using the service on Android Oreo
Sample to display (head-up) notifications on Android
Try using the Emotion API from Android
Try adding text to an image in Scala using the Java standard library
Try using Firebase Cloud Functions on Android (Java)
POST images from Android to PHP using Retrofit
Beginners try using android studio Part 2 (event processing)
[Android] Implement a function to display passwords quickly
Beginners try using android studio Part 1 (Hello World)
Translator using Microsoft Translator Text API on Android ~ Implementation ~
Try to make a music player using Basic Player
Try communication using gRPC on Android + Java server
Try to display hello world with spring + gradle
Display text character by character in Android Surface View
Try using libGDX
Try using Maven
Try using powermock-mockito2-2.0.2
Try using GraalVM
Try using jmockit 1.48
Try using sql-migrate
Try using SwiftLint
Try using Log4j 2.0
[Android] Display of input candidates using List Popup Window
How to add characters to display when using link_to method
Try to implement using Rakuten product search API (easy)
Try image classification using TensorFlow Lite on Android (JAVA)
Try adding SMS authentication (using Twilio) to Keycloak's authentication process
Try to build a Java development environment using Docker
Command to try using Docker for the time being
Try to work with Keycloak using Spring Security SAML (Spring 5)
Try to implement tagging function using rails and js