Debug and fix an issue binding data to views (Associate Android Developer Certification)

In order to use Data binding in views you should configure gradle. Open gradle.build for project.

    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'com.android.databinding:dataBinder:1.0-rc3'
    }

Then open gradle.build(app)

apply plugin: 'com.android.databinding'

Create User POJO:

public class User {
    private final String firstName;
    private final String lastName;
    public User(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
    public String getFirstName() {
        return this.firstName;
    }
    public String getLastName() {
        return this.lastName;
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="user" type="com.gayratrakhimov.databindingsandbox2.User"/>
    </data>
    <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.firstName}"/>
        <TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@{user.lastName}"/>
    </LinearLayout>
</layout>

MainActivity.java

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

        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        User user = new User("Test", "User");
        binding.setUser(user);


    }

Handlers

Create new class:

public class MyHandlers {
    public void onClickFriend(View view) {
        ((TextView) view).setText("Joe Richard");
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable name="handlers" type="com.gayratrakhimov.databindingsandbox2.MyHandlers"/>
        <variable name="user" type="com.gayratrakhimov.databindingsandbox2.User"/>
    </data>
    <LinearLayout
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
        <TextView android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="@{user.firstName}"
           android:onClick="@{handlers.onClickFriend}"/>
    </LinearLayout>
</layout>

MainActivity.java

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

        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        User user = new User("Test", "User");
        binding.setUser(user);
        binding.setHandlers(new MyHandlers());

    }

Examples are taken from: https://developer.android.com/topic/libraries/data-binding/index.html