Record it as a study of Data Binding. This time, it's just the beginning of displaying the text.
Add the following to app gradle
build.gradle
android {
dataBinding {
enabled = true
}
}
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; }
}
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>
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);
}
Next, I would like to implement a dynamic display. next: Dynamically change text using DataBinding (https://qiita.com/kilalabu/items/3cded5e7c560de2ed05c)
https://qiita.com/Omoti/items/a83910a990e64f4dbdf1#step0-%E5%B0%8E%E5%85%A5 Thank you very much
Recommended Posts